问题
I want to show Google Voice Typing IME on my EditText by clicking on Button. So, according to this article and source code I should write this code
inputMethodManager.setInputMethodAndSubtype(IBinder token, String id, InputMethodSubtype subtype)
The problem is: where to find appropriate token. In the source code I saw this
mInputMethodService.getWindow().getWindow().getAttributes().token
It works great, but this code located in InputMetodService superclass, so it has access to InputMethodService instance. But i don't (unfortunately :) ).
Please tell me if you have any suggestions. Thanks.
NOT WORKS: EditText.getWindowToken()
回答1:
Due to security reasons android doesn't allows application to change the inputMethod type. The article you mentioned is for integrating the google IME in custom implemented IME, it is not applicable for applications. you can check documentation of InputMethodManager here
A client application can ask that the system let the user pick a new IME, but can not programmatically switch to one itself. This avoids malicious applications from switching the user to their own IME, which remains running when the user navigates away to another application. An IME, on the other hand, is allowed to programmatically switch the system to another IME, since it already has full control of user input.
you can prompt the user to switch to new IME in your onClick Callback like this:
InputMethodManager imm = (InputMethodManager)
getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showInputMethodPicker();
来源:https://stackoverflow.com/questions/15976837/calling-inputmethodmanager-setinputmethodibinder-token-string-id-outside-the