Pregunta

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.

¿Fue útil?

Solución

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;
            }
        });
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top