按下输入键后隐藏键盘

时间:2014-04-23 17:45:00

标签: android keyboard

简单问题 - 如何在按下回车键/按钮后关闭虚拟键盘?

我已尝试过此Handle “Enter” key on Jelly BeanHow to hide keyboard on enter key,但这些都不适合我。

1 个答案:

答案 0 :(得分:1)

您有两种选择。

使用xml:

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

和代码。

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;
            }
        });
相关问题