Android 按下回车键后隐藏键盘

Android 按下回车键后隐藏键盘,android,keyboard,Android,Keyboard,简单的问题-点击回车键/按钮后如何关闭虚拟键盘 我试过这个,但没有一个对我有效。你有两个选择 使用xml: <EditText android:id="@+id/editText1" android:inputType="text" android:imeOptions="actionDone"/> 第二个链接中的代码应该可以工作,我将提供相同的答案。当您尝试它时发生了什么?由于某种原因,enter键并没有像

简单的问题-点击回车键/按钮后如何关闭虚拟键盘

我试过这个,但没有一个对我有效。

你有两个选择

使用xml:

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

第二个链接中的代码应该可以工作,我将提供相同的答案。当您尝试它时发生了什么?由于某种原因,enter键并没有像通常一样工作。XML代码工作,但Java代码没有效果。我还在想为什么不呢。
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;
            }
        });