Question

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.

Était-ce utile?

La solution

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;
            }
        });
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top