android监听软键盘隐藏

来源:互联网 发布:zbrush mac 中文版 编辑:程序博客网 时间:2024/05/21 19:38

从网上看到的是根据根布局的高度变化来监听软键盘的显示和隐藏,但是如果设置了adjustpan即根布局高度始终不变的时候是无效的,搜了好长时间才找到解决方法。

//获取到根布局,这个根布局和setContenView的布局不一样。rootView = getWindow().getDecorView().findViewById(android.R.id.content);//然后设置布局的监听rootView.getViewTreeObserver().addOnGlobalLayoutListener(this);//让本类继承接口实现方法    @Override    public void onGlobalLayout() {        if(isKeyboardShown(rootView)){        }else {//          Log.e(TAG, "onGlobalLayout: " );//          mImmersionBar.fixMarginAtBottom(true).init();        }    }//这个是最主要的方法,根据根布局可视的高度来判断软键盘的显示和隐藏    private boolean isKeyboardShown(View rootView) {        final int softKeyboardHeight = 100;        Rect r = new Rect();        rootView.getWindowVisibleDisplayFrame(r);        DisplayMetrics dm = rootView.getResources().getDisplayMetrics();        int heightDiff = rootView.getBottom() - r.bottom;        return heightDiff > softKeyboardHeight * dm.density;    }
原创粉丝点击