java 可靠地隐藏软键盘
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13032436/
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
Hiding softkeyboard reliably
提问by stealthjong
I have a application, and need to close the softkeyboard on a rather large amount of actions. For example, when clicking a button, when a new layout is drawn, on screen orientation change, when the controller tellsthe UI to, et cetera. I use the optionsMenuButton to flip view with a ViewFlipper, and obviously I want the keyboard to hide in the flipped view (there is no input field there).
我有一个应用程序,需要在大量操作上关闭软键盘。例如,单击按钮时,绘制新布局时,屏幕方向更改时,控制器告诉UI 时等等。我使用 optionsMenuButton 使用 ViewFlipper 翻转视图,显然我希望键盘隐藏在翻转视图中(那里没有输入字段)。
I've tried these so far and tell why these aren't reliable:
到目前为止,我已经尝试过这些并说明为什么这些不可靠:
This one didn't work because I have lots of editTexts, and other views. I need a more generic one, one that does not require a view as argument, if possible.
这个没有用,因为我有很多 editTexts 和其他视图。如果可能的话,我需要一个更通用的,一个不需要视图作为参数的。
InputMethodManager imm = (InputMethodManager)getSystemService(
Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);
This one does not work at all for me:
这个对我来说根本不起作用:
getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
This one works, but immediately pops the keyboard up again when the view is flipped.
这个有效,但当视图翻转时会立即再次弹出键盘。
InputMethodManager imm = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);
This one works sometimes, but getCurrentFocus() returns null most of the time.
这个有时有效,但大多数情况下 getCurrentFocus() 返回 null。
InputMethodManager inputManager = (InputMethodManager)
Context.getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(),
InputMethodManager.HIDE_NOT_ALWAYS);
This one works only if the keyboard is shown:
这个只有在显示键盘时才有效:
getInstrumentation().sendKeyDownUpSync(KeyEvent.KEYCODE_BACK);
This one does not work with the EditText like the first piece of code, but with the root Layout, which changes on orientation change and everytime the oncreate is called. I have different layout XML for landscape/portrait and normal/large. All the root Layouts have the ID root
. This works nicely the first time, but after that, it doesn't work anymore.
这个不像第一段代码那样与 EditText 一起工作,而是与根布局一起工作,它随着方向变化和每次 oncreate 被调用而改变。我有横向/纵向和普通/大的不同布局 XML。所有的根布局都有 ID root
。这在第一次运行时很好,但在那之后,它不再起作用了。
InputMethodManager inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(findViewById(R.id.root).getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
Bottomline: I've tried hella lot of softkeyboard hiding methods, but none of them seems to work reliably. Is there anymethod for hiding the soft keyboard reliably?
底线:我已经尝试了很多软键盘隐藏方法,但它们似乎都不能可靠地工作。有没有什么方法可以可靠地隐藏软键盘?
采纳答案by MC Emperor
Since you need an EditText
to bring up the keyboard, find the particular one and use the first method you displayed to hide the keyboard:
由于您需要EditText
打开键盘,请找到特定的键盘并使用您显示的第一种方法来隐藏键盘:
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);
However, you will need that EditText
. First, get all views:
但是,您将需要那个EditText
。首先,获取所有视图:
public static ArrayList<View> getAllViewsFromRoots(View...roots) {
ArrayList<View> result = new ArrayList<View>();
for (View root : roots) {
getAllViews(result, root);
}
return result;
}
private static void getAllViews(ArrayList<View> allviews, View parent) {
allviews.add(parent);
if (parent instanceof ViewGroup) {
ViewGroup viewGroup = (ViewGroup)parent;
for (int i = 0; i < viewGroup.getChildCount(); i++) {
getAllViews(allviews, viewGroup.getChildAt(i));
}
}
}
Then, get an EditText
which is visible.
然后,得到一个EditText
可见的。
public static EditText getEditText(View root) {
ArrayList<View> views = getAllViewsFromRoots(root);
for (View view : views) {
if (view instanceof EditText && view.getVisibility() == View.VISIBLE) {
return (EditText) view;
}
}
return null;
}
Call somewhere:
呼叫某处:
Toolkit.getEditText(((ViewParent) findViewById(android.R.id.content)).getChildAt(0));
with that, call the hiding method.
这样,调用隐藏方法。
回答by ayorhan
I think if you handle the null case of getCurrentFocus(), you're good to go. I use the method below and it works like a charm!
我认为如果您处理 getCurrentFocus() 的 null 情况,您就可以开始了。我使用下面的方法,它就像一个魅力!
/* Hide Keyboard */
public static void hideKeyboard(Activity activity){
InputMethodManager inputMethodManager = (InputMethodManager)activity
.getSystemService(Context.INPUT_METHOD_SERVICE);
View focus = activity.getCurrentFocus();
if(focus != null)
inputMethodManager.hideSoftInputFromWindow(focus.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}
回答by ThePCWizard
This one works for me always:
这个总是对我有用:
InputMethodManager im = (InputMethodManager) getSystemService(MyActivity.INPUT_METHOD_SERVICE);
im.hideSoftInputFromWindow(getWindow().getDecorView().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
回答by FDIM
Only this solution worked for me:
只有这个解决方案对我有用:
mActivity.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
I had a dialog with input field, hiding the dialog didnt hide the keyboard on tablet.
我有一个带有输入字段的对话框,隐藏对话框并没有隐藏平板电脑上的键盘。
回答by Anand.B
This should work for most of the cases.
这应该适用于大多数情况。
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(new View(this).getWindowToken(), 0);
No need to worry about the null pointer as well.
也无需担心空指针。