android - 按下回车键后隐藏键盘

标签 android keyboard

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

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

最佳答案

你有两个选择。

使用 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;
            }
        });

关于android - 按下回车键后隐藏键盘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23251605/

相关文章:

android - 使用 Messengers 和 Binders 进行进程间通信

java - 在函数中模拟私有(private)变量以测试 Junit4

android - OnKeyListener 和 OnEditorActionListener 之间的确切区别是什么?

c - 使用 Winapi 键盘输入

c# - 使用键盘上的 Enter 等同于计算器

键盘顶部的Android空栏

php - Android App无法访问Vagrant Homestead托管的Web API

java - 如何在同一 Activity 中为底部导航和抽屉导航创建监听器?

android - textView 不同行的不同文本方向

python - 模拟linux键盘输入