Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/218.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
Android 隐藏键盘-getCurrentFocus()返回null_Android - Fatal编程技术网

Android 隐藏键盘-getCurrentFocus()返回null

Android 隐藏键盘-getCurrentFocus()返回null,android,Android,我想实现一个通用的方法来对任何活动隐藏键盘。我的代码: public static void hide(Activity activity) { if (null == activity) { return; } View view = activity.getCurrentFocus(); if (null == view) { return; } InputMethodManager inputMethodM

我想实现一个通用的方法来对任何活动隐藏键盘。我的代码:

public static void hide(Activity activity) {
    if (null == activity) {
        return;
    }

    View view = activity.getCurrentFocus();
    if (null == view) {
        return;
    }

    InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
    inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
}

它对我的大多数活动都很有效,但不是对所有活动都有效。问题是,有时getCurrentFocus()返回null,即使选中的editText是焦点。此活动可能有什么问题?为什么这个方法返回null?

我在弹出一个片段后隐藏了键盘(不是以前!)。幸运的是,这段代码适用于我的大多数活动。

什么时候在OnOptions ItemSelected(MenuItem项)中调用您的
hide()
方法