Android关于软件关闭和显隐判断。

来源:互联网 发布:机加工成本核算软件 编辑:程序博客网 时间:2024/06/09 21:27

Android中好像没有Api直接获取软件盘获取状态,然后找了许多的贴子,发现都是用isActive()的方法或者是getWindow().getAttributes().softInputMode == WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE 方法来判断是否弹出软键盘,但是我这边得到的值一直是17.所以这两种方法都没有作用。然后经过不断查找,有些是使用判断屏幕高度来判断的,然后我参考了几篇博客,发现能够实现判断是否开启功能。博客地址为
http://blog.csdn.net/javazejian/article/details/52126391 //原创
http://blog.csdn.net/sinat_31311947/article/details/53899166 //参考
提取了他里面的一些方法。

  /**     * 是否显示软件盘     *     * @return     */    public static boolean isSoftInputShown(Activity mActivity) {        return getSupportSoftInputHeight(mActivity) != 0;    }    /**     * 获取软件盘的高度     *     * @return     */    private static int getSupportSoftInputHeight(Activity mActivity) {        Rect r = new Rect();        /**         * decorView是window中的最顶层view,可以从window中通过getDecorView获取到decorView。         * 通过decorView获取到程序显示的区域,包括标题栏,但不包括状态栏。         */        mActivity.getWindow().getDecorView().getWindowVisibleDisplayFrame(r);        //获取屏幕的高度        int screenHeight = mActivity.getWindow().getDecorView().getRootView().getHeight();        //计算软件盘的高度        int softInputHeight = screenHeight - r.bottom;        /**         * 某些Android版本下,没有显示软键盘时减出来的高度总是144,而不是零,         * 这是因为高度是包括了虚拟按键栏的(例如华为系列),所以在API Level高于20时,         * 我们需要减去底部虚拟按键栏的高度(如果有的话)         */        if (Build.VERSION.SDK_INT >= 20) {            // When SDK Level >= 20 (Android L), the softInputHeight will contain the height of softButtonsBar (if has)            softInputHeight = softInputHeight - getSoftButtonsBarHeight(mActivity);        }        return softInputHeight;    }    /**     * 底部虚拟按键栏的高度     *     * @param mActivity     * @return     */    @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)    private static int getSoftButtonsBarHeight(Activity mActivity) {        DisplayMetrics metrics = new DisplayMetrics();        //这个方法获取可能不是真实屏幕的高度        mActivity.getWindowManager().getDefaultDisplay().getMetrics(metrics);        int usableHeight = metrics.heightPixels;        //获取当前屏幕的真实高度        mActivity.getWindowManager().getDefaultDisplay().getRealMetrics(metrics);        int realHeight = metrics.heightPixels;        if (realHeight > usableHeight) {            return realHeight - usableHeight;        } else {            return 0;        }    }

就我目前测试手机而言,没有使用虚拟键盘的手机测试过,都能生效。然后影藏软件盘的方法,基本就是一样了。

   public static void hideSoftKeyboard(Activity activity) {        InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);//        if (view != null) {//            if (view.getWindowToken() != null) {//            //2.调用toggleSoftInput方法隐藏软键盘        if (imm != null)            imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);//        }//            }//        }    } public static void hideSoftKeyboard(View view, Context context) {        //1.得到InputMethodManager对象        InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);        //2.调用hideSoftInputFromWindow方法隐藏软键盘        if (imm != null)            imm.hideSoftInputFromWindow(view.getWindowToken(), 0); //强制隐藏键盘    }

以上两个都行,不过我使用下面的一种方法的时候,关闭软键盘后会有一个英文键盘弹出,然后我就使用的第一种。

原创粉丝点击