質問

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