最有效的隐藏软键盘方法

时间:2021-04-02 23:39:55
    /**
* 隐藏软键盘(只适用于Activity,不适用于Fragment)
*/

public static void hideSoftKeyboard(Activity activity) {
View view = activity.getCurrentFocus();
if (view != null) {
InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}
}

/**
* 隐藏软键盘(可用于Activity,Fragment)
*/

public static void hideSoftKeyboard(Context context, List<View> viewList) {
if (viewList == null) return;

InputMethodManager inputMethodManager = (InputMethodManager) context.getSystemService(Activity.INPUT_METHOD_SERVICE);

for (View v : viewList) {
inputMethodManager.hideSoftInputFromWindow(v.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}
}