How to clear focus and remove keyboard on Android?

后端 未结 6 2060
情歌与酒
情歌与酒 2021-02-01 18:41

I have a EditText control. If I tap it the softkeyboard will popup however when I press \"enter/ok/return\" then the EditText control it still has focus and the keyboard up.

6条回答
  •  无人共我
    2021-02-01 19:14

    In the layout XML file, specify an imeOption on your EditText:

    android:imeOptions="actionGo"
    

    Next, add an action listener to your EditText in the Activity's java file

        mYourEditText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_GO) {
                    // hide virtual keyboard
                    InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.hideSoftInputFromWindow(mYourEditText.getWindowToken(), 0);
                    return true;
                }
                return false;
            }
        });
    

    Where mYourEditText is an EditText object

提交回复
热议问题