调用InputMethodService外InputMethodManager.setInputMethod(令牌的IBinder,字符串ID)。哪里可以找到令牌?令牌、字符串、可以找到、InputM

2023-09-06 14:59:25 作者:好菇凉爸妈造

我想通过点击按钮显示谷歌语音打字输入法在我的EditText。因此,根据这文章和源$ C ​​$ C 我应该写这个$ C $ ç

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)

问题是:在哪里可以找到相应的令牌。在源$ C ​​$ C我看到这个

The problem is: where to find appropriate token. In the source code I saw this

mInputMethodService.getWindow().getWindow().getAttributes().token

它的伟大工程,但位于InputMetodService超类,所以它可以访问InputMethodService比如这个code。但我不(可惜:))。

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.

没有行为: EditText.getWindowToken()

NOT WORKS: EditText.getWindowToken()

推荐答案

由于安全原因,Android不允许应用程序更改INPUTMETHOD类型。你提到的文章是在定制的实现IME集成了谷歌输入法,它不适用于应用程序。你可以检查InputMethodManager 的文档,这里

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

一个客户端应用程序可以要求该系统让用户选择一个新的输入法,,但不能以编程方式切换一个本身。 这就避免了从用户切换到自己的输入法,当用户导航离开到另一个应用程序,它仍在运行恶意应用程序。 IME的,另一方面,被允许在系统编程切换到另一个输入法,因为它已经具有用户输入的完全控制。

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.

您可以提示用户切换到新的输入法在你的onClick回调是这样的:

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();