Android手柄'搜索'按钮按下自定义键盘

And*_*rey 5 android custom-keyboard

我正在开发自己的自定义键盘.

如果我们的键盘用IME_ACTION_SEARCH参数打开,如何处理"搜索"按钮?

我有以下代码,但不幸的是在搜索案例中它不起作用.在正常情况下使用完成按钮它工作正常.

        final int options = this.getCurrentInputEditorInfo().imeOptions;
        final int actionId = options & EditorInfo.IME_MASK_ACTION;

        switch (actionId) {
            case EditorInfo.IME_ACTION_SEARCH:
                ic.sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_SEARCH));
                break;
            default:
                ic.sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_ENTER));
        }
Run Code Online (Sandbox Code Playgroud)

谢谢

And*_*rey 8

我找到了解决方案:

endDefaultEditorAction(true);
Run Code Online (Sandbox Code Playgroud)

这是一种方法 InputMethodService

完整的代码是:

    case Keyboard.KEYCODE_DONE:
        final int options = this.getCurrentInputEditorInfo().imeOptions;
        final int actionId = options & EditorInfo.IME_MASK_ACTION;

        switch (actionId) {
            case EditorInfo.IME_ACTION_SEARCH:
                sendDefaultEditorAction(true);
                break;
            case EditorInfo.IME_ACTION_GO:
                sendDefaultEditorAction(true);
                break;
            case EditorInfo.IME_ACTION_SEND:
                sendDefaultEditorAction(true);
                break;
            default:
                ic.sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_ENTER));
        }

        break;
Run Code Online (Sandbox Code Playgroud)