android实现回车键的监听

1.布局文件中的edittext:设置属性

android:singleLine="true"
android:imeOptions="actionSearch"

2.java文件中

editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
                if (i == EditorInfo.IME_ACTION_UNSPECIFIED) {
                    Toast.maketext(this,"你点击了回车",Toast.LENGTH_SHORT).show();
                    //隐藏软键盘
                    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.hideSoftInputFromWindow(getWindow().getDecorView().getWindowToken(), 0);
                }
                return false;
            }
        });

猜你喜欢

转载自blog.csdn.net/qq_37918409/article/details/81665910