以编程方式从自定义Android键盘更改键盘

时间:2012-06-26 10:05:11

标签: android ime android-input-method

我创建了一个Android自定义键盘。按下按钮后,我希望将键盘更改回以前的键盘,可以使用InputMethodManager.setInputMethod(IBinder token, String id);

但是,我无法确定从何处获取令牌 - 使用getCurrentInputBinding().getConnectionToken()无效。

有谁知道在哪里找到令牌?

谢谢,

3 个答案:

答案 0 :(得分:1)

事实证明switchInputMethod(String id)方法有效 - 不需要该令牌。

答案 1 :(得分:1)

您可以通过view.getWindowToken()从视图中获取令牌。

答案 2 :(得分:0)

您可以使用此方法获取令牌并激活上次使用的键盘

 private fun switchToLastKeyboard() {
        try {
            val imm: InputMethodManager =
                this.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
            val token = this.window.window!!.attributes.token
            //imm.setInputMethod(token, LATIN);
            imm.switchToLastInputMethod(token)
        } catch (t: Throwable) { // java.lang.NoSuchMethodError if API_level<11
            Log.i("TAG", "onCreateInputView: Throwable " + t.message)
        }

    }
相关问题