IME“ Enter”键的行为类似于FB Messenger中的“ Send”

时间:2018-11-25 17:03:53

标签: java android android-softkeyboard ime

使用 FB Messenger 时,

在我的自定义键盘中,按“ Enter”键->发送消息。

但是在Google键盘中,按“ Enter”键->转到新行。

如何按自定义键盘的“ Enter”键转到新行?

                EditorInfo editorInfo = getCurrentInputEditorInfo();
                int imeOptions = editorInfo != null ? editorInfo.imeOptions : -77;
                switch (imeOptions & (EditorInfo.IME_MASK_ACTION|EditorInfo.IME_FLAG_NO_ENTER_ACTION)) {
                    case EditorInfo.IME_ACTION_GO:
                        ic.performEditorAction(EditorInfo.IME_ACTION_GO);
                        break;
                    case EditorInfo.IME_ACTION_NEXT:
                        ic.performEditorAction(EditorInfo.IME_ACTION_NEXT);
                        break;
                    case EditorInfo.IME_ACTION_SEARCH:
                        ic.performEditorAction(EditorInfo.IME_ACTION_SEARCH);
                        break;
                    case EditorInfo.IME_ACTION_SEND:
                        ic.performEditorAction(EditorInfo.IME_ACTION_SEND);
                        break;
                    default:
                        // seems like passing here but sending the message
                        ic.sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_ENTER));
                        break;
                }

1 个答案:

答案 0 :(得分:0)

因此,您可以做的是按如下键收听:

 @Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_ENTER){
        // Get the text in the TextView or whatever and append the "\n" to it.
        textView.setText(textView.getText.toString() + "\n");
    }
}

此方法侦听何时按下某个键,然后检查所按下的键是否为Enter键。然后,它通过执行\n添加新行。我希望这会有所帮助。