Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/180.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 可靠隐藏软键盘_Java_Android_Android Softkeyboard - Fatal编程技术网

Java 可靠隐藏软键盘

Java 可靠隐藏软键盘,java,android,android-softkeyboard,Java,Android,Android Softkeyboard,我有一个应用程序,需要关闭大量操作的软键盘。例如,单击按钮时,绘制新的布局时,屏幕方向改变时,控制器告诉用户界面,等等。 我使用选项菜单按钮使用ViewFlipper翻转视图,显然我希望键盘隐藏在翻转视图中(那里没有输入字段) 到目前为止,我已经尝试过这些方法,并解释了这些方法不可靠的原因: 这一个不起作用,因为我有很多编辑文本和其他视图。我需要一个更通用的,一个不需要视图作为参数的,如果可能的话 InputMethodManager imm = (InputMethodManager)getS

我有一个应用程序,需要关闭大量操作的软键盘。例如,单击按钮时,绘制新的布局时,屏幕方向改变时,控制器告诉用户界面,等等。 我使用选项菜单按钮使用ViewFlipper翻转视图,显然我希望键盘隐藏在翻转视图中(那里没有输入字段)

到目前为止,我已经尝试过这些方法,并解释了这些方法不可靠的原因:

这一个不起作用,因为我有很多编辑文本和其他视图。我需要一个更通用的,一个不需要视图作为参数的,如果可能的话

InputMethodManager imm = (InputMethodManager)getSystemService(
  Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);
这个对我来说根本不起作用:

getWindow().setSoftInputMode(
  WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
这一个工作,但立即弹出键盘再次当视图翻转

InputMethodManager imm = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);
这个函数有时有效,但getCurrentFocus()大部分时间返回null

InputMethodManager inputManager = (InputMethodManager)            
Context.getSystemService(Context.INPUT_METHOD_SERVICE); 
inputManager.hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(),      
InputMethodManager.HIDE_NOT_ALWAYS);
此选项仅在显示键盘时有效:

getInstrumentation().sendKeyDownUpSync(KeyEvent.KEYCODE_BACK);
与第一段代码不同,这段代码不适用于EditText,而是适用于根布局,根布局随着方向的改变而改变,每次调用oncreate时都会改变。对于横向/纵向和普通/大型,我有不同的布局XML。所有根布局都具有ID
root
。这在第一次很好地工作,但是在那之后,它就不再工作了

InputMethodManager inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(findViewById(R.id.root).getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);

底线:我尝试了很多软键盘隐藏方法,但似乎没有一种能可靠地工作有什么方法可以可靠地隐藏软键盘吗?

这一方法对我来说总是有效的:

InputMethodManager im = (InputMethodManager) getSystemService(MyActivity.INPUT_METHOD_SERVICE);
im.hideSoftInputFromWindow(getWindow().getDecorView().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);

我认为,如果处理getCurrentFocus()的空情况,就可以开始了。我使用下面的方法,它就像一个魅力

     /* 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);
    }

由于需要使用
EditText
打开键盘,请找到特定的方法,然后使用显示的第一种方法隐藏键盘:

InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);
但是,您需要
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));
        }
    }
}
打电话到某处:

Toolkit.getEditText(((ViewParent) findViewById(android.R.id.content)).getChildAt(0));

这样,调用隐藏方法。

只有这个解决方案对我有效:

mActivity.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

我有一个带有输入字段的对话框,隐藏对话框并没有隐藏平板电脑上的键盘。

这应该适用于大多数情况

InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(new View(this).getWindowToken(), 0);

无需担心空指针。

使用viewflipper时,视图焦点为空,因此键盘不会隐藏。可能是viewFlipper有问题?编辑:可能不会,旋转屏幕不会隐藏键盘。您确定正在传递正确的活动并在正确的位置调用该方法吗?我认为如果键盘打开,视图焦点不应该为空。经过一些测试,这似乎是最可靠的方法。你能发送代码示例吗?我应该把Toolkit.getEditText(((ViewParent)findViewById(android.R.id.content)).getChildAt(0))放在哪里?嫁给我!!我一直在寻找一个好的解决办法。看来我找到了一个好(短)的。谢谢
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(new View(this).getWindowToken(), 0);