简体   繁体   English

Android 处理自定义键盘上的“搜索”按钮

[英]Android handle 'search' button press on custom keyboard

I'am developing my own custom keyboard.我正在开发自己的自定义键盘。

How to handle 'search' button press in case if our keyboard opened with IME_ACTION_SEARCH parameter?如果我们的键盘以IME_ACTION_SEARCH参数打开,如何处理“搜索”按钮按下?

I have following code, but unfortunately in search case it's not working.我有以下代码,但不幸的是在搜索情况下它不起作用。 In regular situation with Done button it working good.在正常情况下,“完成”按钮运行良好。

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

Thanks谢谢

I found the solution to do it:我找到了解决方案:

endDefaultEditorAction(true);

it's a method of InputMethodService它是InputMethodService的一个方法

The full code is:完整代码是:

    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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM