Simply question - how to close virtual keyboard after hitting enter key/button?

I've tried this Handle “Enter” key on Jelly Bean and How to hide keyboard on enter key, but none of this works for me.

有帮助吗?

解决方案

You have two options.

With xml:

 <EditText
            android:id="@+id/editText1"
            android:inputType="text"
            android:imeOptions="actionDone"/>

and with code.

edittext.setOnEditorActionListener(new OnEditorActionListener() {
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (event != null&& (event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {
                    InputMethodManager in = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                    in.hideSoftInputFromWindow(edittext.getApplicationWindowToken(),InputMethodManager.HIDE_NOT_ALWAYS);
                }
                return false;
            }
        });
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top