Android handle 'search' button press on custom keyboard

假如想象 提交于 2021-02-04 15:40:20

问题


I'am developing my own custom keyboard.

How to handle 'search' button press in case if our keyboard opened with IME_ACTION_SEARCH parameter?

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


回答1:


I found the solution to do it:

endDefaultEditorAction(true);

it's a method of 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;


来源:https://stackoverflow.com/questions/30698657/android-handle-search-button-press-on-custom-keyboard

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