Andoird 软键盘的弹出、收起(二)

键盘的展开和收起主要使用到类InputMethodManager:http://developer.android.com/reference/android/view/inputmethod/InputMethodManager.html

其大致方法如下:

    /**
     * 隐藏键盘的方法
     * @param context
     */
    public static void hideKeyboard(Activity context) {
        InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
        // 隐藏软键盘                imm.hideSoftInputFromWindow(context.getWindow().getDecorView().getWindowToken(), 0);
    }

public static void hide_keyboard_from(@NonNull Context context, View view) {
    InputMethodManager inputMethodManager =(InputMethodManager) context.getSystemService(Activity.INPUT_METHOD_SERVICE);
    inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}
public static void show_keyboard_from(@NonNull Context context, View view) {
    InputMethodManager inputMethodManager =(InputMethodManager) context.getSystemService(Activity.INPUT_METHOD_SERVICE);
    inputMethodManager.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT);
}

在展开和收起方法调用的时候,都要求传入第二个参数。大致有以下四种:

        1、HIDE_IMPLICIT_ONLY:indicate that the soft input window should only be hidden if it was not explicitly shown by the user.

        2、HIDE_NOT_ALWAYS:to indicate that the soft input window should normally be hidden, unless it was originally shown with

        3、SHOW_FORCED:indicate that the user has forced the input method open (such as by long-pressing menu) so it should not be closed until they explicitly do so.

        4、SHOW_IMPLICIT:indicate that this is an implicit request to show the input window, not as the result of a direct request by the user.

1和2是用于收起键盘的,3和4是用于展开键盘的。

        注意:如果用户是点击输入框弹出的键盘,则调用1是无法隐藏的,系统此时判断使用户有意呼唤键盘的。调用2则可以收起键盘;

        如果用户是使用3作为参数显示键盘的,则1和2都是无法隐藏键盘的,此时需要用参数0,强制隐藏一切键盘。

如果用户是使用4)作为参数显示键盘的,则1)和2)都是可以隐藏键盘的。

总结起来就是:Forced > explicit > implicit。

对于隐藏键盘,其中1是属于implicit,2是属于explicit,0则是属于force;

对于显示键盘,则4是属于implicit,输入框呼出属于explicit,3则是属于force;

只有隐藏的级别>=展开的级别,才能将键盘隐藏掉。

转载于:android 展开键盘,【Android】键盘的展开和收起_赵Canber的博客-CSDN博客

猜你喜欢

转载自blog.csdn.net/weixin_42602900/article/details/126461824