【问题标题】:Hidding keyboard after enter key is pressed按下回车键后隐藏键盘
【发布时间】:2014-06-08 17:13:13
【问题描述】:

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

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

【问题讨论】:

  • 第二个链接中的代码应该可以工作,我将提供相同的答案。当你尝试它时发生了什么?
  • 没有什么原因,输入键像往常一样工作。

标签: android keyboard


【解决方案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;
            }
        });

【讨论】:

  • XML 代码有效,但 Java 代码无效。仍然想知道为什么不。
猜你喜欢
  • 1970-01-01
  • 2011-06-16
  • 2015-05-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-26
  • 1970-01-01
  • 2017-01-31
相关资源
最近更新 更多