通用键盘解雇

时间:2014-07-08 08:34:11

标签: android

通常在分离片段时,软键盘仍然存在。片段的视图包含几个EditText字段 - 是否有一种方法可以在不传入最后一个焦点的EditText的情况下一般性地关闭键盘?

1 个答案:

答案 0 :(得分:0)

在片段的onDestroy()方法上,或通过委托给父活动的自己的回调,您应该调用下面的方法。没有必要传递视图:

public static void hideSoftKeyboard(Activity activity) {
    InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
    inputMethodManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);
}

public static void showSoftKeyboard(Activity activity, View view) {
    activity.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
    InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
    inputMethodManager.showSoftInput(view, 0);
}

public static boolean isSoftKeyboardShowing(Activity activity) {
    InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
    return inputMethodManager.isActive();
}