Android: catching action for keyboard enter button being pressed

偶尔善良 提交于 2019-12-04 05:10:07

You respond to ENTER differently than normal keys, try something like this:

editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {

        switch (actionId) {

            case EditorInfo.IME_ACTION_DONE:
                ...
                return true;

            default:
                return false;
        }
    }
});

You can specify which action is performed by ENTER by specifiying android:imeOptions in XML or by calling editText.setImeOptions(...) with one of the constants in EditorInfo. imeOptions also changes how the ENTER key looks! For example if you set imeOptions to actionSearch then the ENTER key will look like a search button.

You can find a list of all possible values for imeOptions along with a description here.

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