Calling InputMethodManager.setInputMethod(IBinder token, String id) outside the InputMethodService. Where to find token?

你离开我真会死。 提交于 2019-12-22 09:45:50

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!