hideSoftInputFromWindow
,传入包含你的焦点视图的窗口的令牌,来强制Android隐藏虚拟键盘。// 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
Kotlin语法// Only runs if there is a view that is currently focused
this.currentFocus?.let { view ->
val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as? InputMethodManager
imm?.hideSoftInputFromWindow(view.windowToken, 0)
}
getSystemService()
需要一个Context
和一个serviceClass Class
。对于上下文,我可以调用requiredContext
,但是对于serviceClass
呢? - capo11Fragment
中使用getActivity().getSystemService()
非常有效。 - Captain Jack SparrowKeyboard.hide()
。结束。非常感谢。但是Android存在一个问题。您必须使用InputMethodManager
来隐藏键盘。好吧,这是Android对键盘的API。但是!您需要一个Context
才能访问IMM。现在我们有一个问题。我可能想要从一个静态或实用类中隐藏键盘,而这些类没有任何Context
的用途或需求。更糟糕的是,IMM要求您指定要从哪个View
(甚至更糟糕的是,要从哪个Window
)隐藏键盘。
这就是隐藏键盘如此具有挑战性的原因。亲爱的Google:当我查找蛋糕食谱时,地球上没有任何一个RecipeProvider
会拒绝向我提供食谱,除非我首先回答蛋糕将由谁食用和在哪里食用!
这个悲伤的故事以丑陋的真相告终:为了隐藏Android键盘,您需要提供两种身份证明:一个Context
和一个View
或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);
}
请注意,此实用程序方法仅在从Activity
中调用时有效!上述方法调用目标Activity
的getCurrentFocus
以获取正确的窗口令牌。
但是如果您想要隐藏托管在DialogFragment
中的EditText
中的键盘怎么办?您不能使用上面的方法来完成:
hideKeyboard(getActivity()); //won't work
这样做是行不通的,因为你将会传递一个指向Fragment
宿主Activity
的引用,而当Fragment
显示时,该Activity
没有任何聚焦控件!哇!因此,为了从片段中隐藏键盘,我使用更低级、更常见和更丑陋的方法:
public static void hideKeyboardFrom(Context context, View view) {
InputMethodManager imm = (InputMethodManager) context.getSystemService(Activity.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
以下是从追寻解决方案的更多时间中获取的一些额外信息:
关于windowSoftInputMode
还有一个需要注意的争议点。默认情况下,Android会自动将初始焦点分配给Activity中的第一个EditText或可聚焦控件。显然,输入法(通常是软键盘)会响应焦点事件并显示自己。当在AndroidManifest.xml中设置windowSoftInputMode属性为stateAlwaysHidden时,会指示键盘忽略此自动分配的初始焦点。请注意保留HTML标签。
<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();
getRootView()
,而不仅仅是 getView()
? - ilw隐藏软键盘的另一个有用方法是:
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);
在showFlag
的位置处传递HIDE_IMPLICIT_ONLY
,在hiddenFlag
的位置处传递0
。这将强制关闭软键盘。
Meier 的解决方案对我也有用。在我的情况下,我的应用程序的顶层是一个选项卡宿主,并且我想在切换选项卡时隐藏关键字 - 我从选项卡宿主视图获取窗口令牌。
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(InputType.TYPE_NULL);
editView.setInputType(InputType.TYPE_NULL);
。 - Bostone更新: 我不知道为什么这个解决方案不再起作用了(我刚在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) {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(input.getWindowToken(), 0);
}
input.getContext().getSystemService(Context.INPUT_METHOD_SERVICE)
。 - CoolMindinput.setInputType(0);
。这改变了键盘行为和 EditText
的 inputType
。 - CoolMind如果其他答案不能满足你的要求,你还有另一种手动控制键盘的方式。
创建一个函数来管理一些EditText
的属性:
public void setEditTextFocus(boolean isFocused) {
searchEditText.setCursorVisible(isFocused);
searchEditText.setFocusable(isFocused);
searchEditText.setFocusableInTouchMode(isFocused);
if (isFocused) {
searchEditText.requestFocus();
}
}
然后,确保在 EditText
的 onFocus 事件中打开/关闭键盘:
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 ... */
}
fragment.getActivity().getSystemService();
。 - Johan S