当前位置: 移动技术网 > IT编程>移动开发>Android > Android关于软件关闭和显隐判断。

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

2019年04月18日  | 移动技术网IT编程  | 我要评论

吉祥坊89168澳门官方,密阳下载,瘢痕疙瘩王根会

android中好像没有api直接获取软件盘获取状态,然后找了许多的贴子,发现都是用isactive()的方法或者是getwindow().getattributes().softinputmode == windowmanager.layoutparams.soft_input_state_visible 方法来判断是否弹出软键盘,但是我这边得到的值一直是17.所以这两种方法都没有作用。然后经过不断查找,有些是使用判断屏幕高度来判断的,然后我参考了几篇博客,发现能够实现判断是否开启功能。
提取了他里面的一些方法。

  /**
     * 是否显示软件盘
     *
     * @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); //强制隐藏键盘
    }

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

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网