Android 对话框关闭后隐藏软键盘
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11633792/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Hide soft keyboard after dialog dismiss
提问by Dmytro Zarezenko
I want to hide soft keyboard after AlertDialog dismiss, but it's still visible. Here is my code:
我想在 AlertDialog 关闭后隐藏软键盘,但它仍然可见。这是我的代码:
alert = new AlertDialog.Builder(MyActivity.this);
imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
alert.setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialog) {
imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
}
});
回答by Rajeshwar
In Manifest xml
在清单 xml 中
android:windowSoftInputMode="stateAlwaysHidden"
It will automatically hide soft keyboard on Dismiss of
Dialog
它会在关闭时自动隐藏软键盘
Dialog
回答by NordicShaw
I met the same problem. Solved it by doing like this. It doesn't need any reference:
我遇到了同样的问题。通过这样做解决了它。它不需要任何参考:
imm.hideSoftInputFromWindow(getWindow().getDecorView()
.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
回答by Miika Pakarinen
I had a similar problem when closing an alert dialog. This seems to do the trick for me.
关闭警报对话框时我遇到了类似的问题。这似乎对我有用。
Inside your DialogFragment
在你的 DialogFragment 里面
public static void closeKB(final View view)
{
caller.postDelayed(new Runnable() {
@Override
public void run() {
InputMethodManager imm = (InputMethodManager) view.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}
}, 1);
}
@Override
public void onDismiss(DialogInterface dialog)
{
super.onDismiss(dialog);
View view = getActivity().getCurrentFocus();
if (view != null)
{
closeKB(view);
}
}
回答by ScouseChris
I use this method:
我用这个方法:
IBinder token = searchTextEntry.getWindowToken();
( ( InputMethodManager ) getSystemService( Context.INPUT_METHOD_SERVICE ) ).hideSoftInputFromWindow( token, 0 );
Where searchTextEntry
is the name of my EditText
reference.
searchTextEntry
我的EditText
参考名称在哪里。
回答by Karthik Sankar
This works! This will close the keyboard after dialog dismiss
这有效!这将在对话框关闭后关闭键盘
InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);
回答by Yusuf Ya?ar
protected void hideKeyboard() {
final Activity activity = getActivity();
final View view = activity != null ? activity.getCurrentFocus() : null;
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
if (view != null) {
InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm != null)
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
}
}, 1);
}
@Override
public void onDismiss(DialogInterface dialog) {
super.onDismiss(dialog);
hideKeyboard();
}
回答by user3130847
All these advices to use InputMethodManager
are somewhat vague - where exactly to call it,
and they do not work at least for me.
Yes, keyboard disappears but then the app crashes!?
The main problem is that hiding of keyboard happens at the same time when dialog is disappearing.
所有这些使用的建议InputMethodManager
都有些含糊——具体在哪里调用它,
至少对我来说它们不起作用。
是的,键盘消失了,但随后应用程序崩溃了!?
主要问题是在对话框消失的同时发生键盘的隐藏。
To avoid it dialog.dismiss()
should be called in view.postDelayed()
after imm.hideSoftInputFromWindow()
and in my case I set delay as 150.
为了避免它dialog.dismiss()
应该在view.postDelayed()
之后调用imm.hideSoftInputFromWindow()
,在我的情况下,我将延迟设置为 150。