简体   繁体   English

通用键盘解雇

[英]Generic keyboard dismissal

Often when a fragment is detached, the soft keyboard remains. 通常,当片段被分离时,软键盘仍然保留。 The fragment's view contains several EditText fields - is there a way to generically dismiss the keyboard without passing in the EditText which last had the focus? 片段的视图包含几个EditText字段-是否有一种方法可以在不传递最后一个具有焦点的EditText的情况下将键盘关闭?

On your fragment's onDestroy() method, or via your own callback delegated to the parent activity, you should call the method below. 在片段的onDestroy()方法上,或通过委派给父活动的回调,您应调用以下方法。 There is no need to pass in a view: 无需传递视图:

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();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM