我有一个 EditText
和a Button
在我的布局中。
在编辑字段中写入并单击后 Button
,我想隐藏虚拟键盘。我假设这是一段简单的代码,但我在哪里可以找到它的一个例子?
我有一个 EditText
和a Button
在我的布局中。
在编辑字段中写入并单击后 Button
,我想隐藏虚拟键盘。我假设这是一段简单的代码,但我在哪里可以找到它的一个例子?
为了帮助澄清这种疯狂,我想首先代表所有Android用户道歉,谷歌对软键盘的彻头彻尾的荒谬处理。对于同样简单的问题,有很多答案,每个不同的原因是因为这个API与Android中的许多其他答案一样,设计非常糟糕。我可以想到没有礼貌的方式陈述它。
我想隐藏键盘。我期望为Android提供以下声明: Keyboard.hide()
。结束。非常感谢你。但Android存在问题。你必须使用 InputMethodManager
隐藏键盘。好的,很好,这是Android的键盘API。但!你需要有一个 Context
为了访问IMM。现在我们遇到了问题。我可能想要将键盘隐藏在没有任何用途或需要的静态或实用程序类中 Context
。或者更糟糕的是,IMM要求您指定什么 View
(甚至更糟,是什么 Window
)你想隐藏键盘FROM。
这使得隐藏键盘变得如此具有挑战性。亲爱的谷歌:当我查找蛋糕的食谱时,没有 RecipeProvider
在地球上拒绝向我提供食谱,除非我第一次回答世界卫生组织,蛋糕将被吃掉,它会被吃掉!
这个悲伤的故事以丑陋的事实结束:要隐藏Android键盘,您需要提供两种形式的识别:a Context
或者a View
或者a Window
。
我已经创建了一个静态实用程序方法,可以非常稳定地完成工作,只要你从中调用它 Activity
。
public static void hideKeyboard(Activity activity) {
InputMethodManager imm = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
//Find the currently focused view, so we can grab the correct window token from it.
View view = activity.getCurrentFocus();
//If no view currently has focus, create a new one, just so we can grab a window token from it
if (view == null) {
view = new View(activity);
}
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
请注意,此实用程序方法仅在从a调用时才有效 Activity
!上面的方法调用 getCurrentFocus
目标 Activity
获取正确的窗口令牌。
但是假设你想隐藏键盘 EditText
托管在 DialogFragment
?你不能使用上面的方法:
hideKeyboard(getActivity()); //won't work
这不起作用,因为你将传递一个引用 Fragment
的主持人 Activity
,这将是没有集中控制 Fragment
显示!哇!因此,为了隐藏键盘中的碎片,我采用较低级别,更常见,更丑陋:
public static void hideKeyboardFrom(Context context, View view) {
InputMethodManager imm = (InputMethodManager) context.getSystemService(Activity.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
以下是从更多时间浪费在追逐此解决方案中收集到的一些其他信息:
关于windowSoftInputMode
还有另一个争论点需要注意。默认情况下,Android会自动将初始焦点分配给第一个 EditText
或者你可以集中控制 Activity
。由此可见,InputMethod(通常是软键盘)将通过显示自身来响应焦点事件。该 windowSoftInputMode
属性 AndroidManifest.xml
,当设置为 stateAlwaysHidden
,指示键盘忽略此自动分配的初始焦点。
<activity
android:name=".MyActivity"
android:windowSoftInputMode="stateAlwaysHidden"/>
几乎令人难以置信的是,当您触摸控件时,它似乎无法阻止键盘打开(除非 focusable="false"
和/或 focusableInTouchMode="false"
分配给控件)。显然,windowSoftInputMode设置仅适用于自动焦点事件,而不适用于触发触摸事件触发的事件。
因此, stateAlwaysHidden
确实很糟糕。它也许应该被称为 ignoreInitialFocus
代替。
希望这可以帮助。
更新:获取窗口令牌的更多方法
如果没有焦点视图(例如,如果您刚刚更改了片段,则会发生),还有其他视图将提供有用的窗口令牌。
这些是上述代码的替代方案 if (view == null) view = new View(activity);
这些并未明确提及您的活动。
在片段类中:
view = getView().getRootView().getWindowToken();
给出一个片段 fragment
作为参数:
view = fragment.getView().getRootView().getWindowToken();
从您的内容正文开始:
view = findViewById(android.R.id.content).getRootView().getWindowToken();
更新2:清除焦点以避免在从后台打开应用程序时再次显示键盘
将此行添加到方法的末尾:
view.clearFocus();
您可以强制Android使用隐藏虚拟键盘 InputMethodManager,打电话 hideSoftInputFromWindow
,传入包含焦点视图的窗口的标记。
// 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
作为第二个参数,确保您只在用户没有明确强制它出现时隐藏键盘(通过按住菜单)。
注意: 如果你想在Kotlin中这样做,请使用:
context?.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
隐藏软键盘也很有用:
getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN
);
这可用于在用户实际触摸editText视图之前抑制软键盘。
我还有一个隐藏键盘的解决方案:
InputMethodManager imm = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);
在这里通过 HIDE_IMPLICIT_ONLY
在...的位置 showFlag
和 0
在...的位置 hiddenFlag
。它会强行关闭软键盘。
Meier的解决方案也适用于我。在我的情况下,我的应用程序的顶级是tabHost,我想在切换标签时隐藏关键字 - 我从tabHost视图中获取窗口标记。
tabHost.setOnTabChangedListener(new OnTabChangeListener() {
public void onTabChanged(String tabId) {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(tabHost.getApplicationWindowToken(), 0);
}
}
请尝试以下代码 onCreate()
EditText edtView=(EditText)findViewById(R.id.editTextConvertValue);
edtView.setInputType(0);
更新: 我不知道为什么这个解决方案不再起作用(我刚刚在Android 23上测试过)。请使用解决方案 Saurabh Pareek 代替。这里是:
InputMethodManager imm = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE);
//Hide:
imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);
//Show
imm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, 0);
老答案:
//Show soft-keyboard:
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
//hide keyboard :
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
protected void hideSoftKeyboard(EditText input) {
input.setInputType(0);
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(input.getWindowToken(), 0);
}
如果这里的所有其他答案对您不起作用,那么还有另一种手动控制键盘的方法。
用它创建一个函数来管理一些 EditText
的属性:
public void setEditTextFocus(boolean isFocused) {
searchEditText.setCursorVisible(isFocused);
searchEditText.setFocusable(isFocused);
searchEditText.setFocusableInTouchMode(isFocused);
if (isFocused) {
searchEditText.requestFocus();
}
}
然后,确保onFocus的 EditText
你打开/关闭键盘:
searchEditText.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (v == searchEditText) {
if (hasFocus) {
// Open keyboard
((InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE)).showSoftInput(searchEditText, InputMethodManager.SHOW_FORCED);
} else {
// Close keyboard
((InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(searchEditText.getWindowToken(), 0);
}
}
}
});
现在,每当你想手动打开键盘时调用:
setEditTextFocus(true);
关闭电话:
setEditTextFocus(false);
Saurabh Pareek 到目前为止有最好的答案。
不过,不妨使用正确的旗帜。
/* hide keyboard */
((InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE))
.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, 0);
/* show keyboard */
((InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE))
.toggleSoftInput(0, InputMethodManager.HIDE_IMPLICIT_ONLY);
实际使用的例子
/* click button */
public void onClick(View view) {
/* hide keyboard */
((InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE))
.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, 0);
/* start loader to check parameters ... */
}
/* loader finished */
public void onLoadFinished(Loader<Object> loader, Object data) {
/* parameters not valid ... */
/* show keyboard */
((InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE))
.toggleSoftInput(0, InputMethodManager.HIDE_IMPLICIT_ONLY);
/* parameters valid ... */
}
从搜索到这里,我找到了一个适合我的答案
// Show soft-keyboard:
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
// Hide soft-keyboard:
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);