关闭/隐藏Android的软键盘 [英] Close/hide the Android Soft Keyboard
问题描述
我在我的布局的EditText
和按钮
。写在编辑栏,并点击按钮后
,我想隐藏虚拟键盘。我认为有一个简单,一两班轮做到这一点。我在哪里可以找到它的一个例子吗?
I have an EditText
and a Button
in my layout. After writing in the edit field and clicking on the Button
, I want to hide the virtual keyboard. I assume that there's a simple, one- or two-liner to make this happen. Where can I find an example of it?
推荐答案
您可以强制使用Android的隐藏虚拟键盘的<一个href="http://developer.android.com/reference/android/view/inputmethod/InputMethodManager.html">InputMethodManager,调用<一href="http://developer.android.com/reference/android/view/inputmethod/InputMethodManager.html#hideSoftInputFromWindow%28android.os.IBinder,%20int%29"><$c$c>hideSoftInputFromWindow$c$c>,传递包含您的集中视图窗口的记号。
You can force Android to hide the virtual keyboard using the InputMethodManager, calling hideSoftInputFromWindow
, passing in the token of the window containing your focused view.
// Check if no view has focus:
View view = this.getCurrentFocus();
if (view != null) {
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
这将迫使键盘被隐藏于所有情况。在某些情况下,你将要在 InputMethodManager.HIDE_IMPLICIT_ONLY
传递的第二个参数,以确保您只隐藏键盘,当用户没有明确强制其出庭(通过持有下拉菜单)。
This will force the keyboard to be hidden in all situations. In some cases you will want to pass in InputMethodManager.HIDE_IMPLICIT_ONLY
as the second parameter to ensure you only hide the keyboard when the user didn't explicitly force it to appear (by holding down menu).
这篇关于关闭/隐藏Android的软键盘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!