美文网首页
系统键盘监控

系统键盘监控

作者: 霁逸lei | 来源:发表于2021-06-15 22:26 被阅读0次

    监控系统键盘弹起隐藏

    SoftKeyBoardListener.setListener(this,new SoftKeyBoardListener.OnSoftKeyBoardChangeListener() {
                @Override
                public void keyBoardShow(int height) {
                    Log.d("TAG", "keyBoardShow height:" + height);
                }
    
                @Override
                public void keyBoardHide(int height) {
                    Log.d("TAG", "keyBoardHide height:" + height);
                }
            });
    
    
    public class SoftKeyBoardListener {
        private View rootView;//activity的根视图
        int rootViewVisibleHeight;//纪录根视图的显示高度
        private OnSoftKeyBoardChangeListener onSoftKeyBoardChangeListener;
    
        public SoftKeyBoardListener(Activity activity) {
            //获取activity的根视图
            rootView = activity.getWindow().getDecorView();
    
            //监听视图树中全局布局发生改变或者视图树中的某个视图的可视状态发生改变
            rootView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                @Override
                public void onGlobalLayout() {
                    //获取当前根视图在屏幕上显示的大小
                    Rect r = new Rect();
                    rootView.getWindowVisibleDisplayFrame(r);
                    //r.height()在全屏和存在虚拟按键时计算高度会出问题
                    int visibleHeight = r.bottom;
                    if (rootViewVisibleHeight == 0) {
                        rootViewVisibleHeight = visibleHeight;
                        return;
                    }
    
                    //根视图显示高度没有变化,可以看作软键盘显示/隐藏状态没有改变
                    if (rootViewVisibleHeight == visibleHeight) {
                        return;
                    }
    
                    //根视图显示高度变小超过200,可以看作软键盘显示了
                    if (rootViewVisibleHeight - visibleHeight > 200) {
                        if (onSoftKeyBoardChangeListener != null) {
                            onSoftKeyBoardChangeListener.keyBoardShow(rootViewVisibleHeight - visibleHeight);
                        }
                        rootViewVisibleHeight = visibleHeight;
                        return;
                    }
    
                    //根视图显示高度变大超过200,可以看作软键盘隐藏了
                    if (visibleHeight - rootViewVisibleHeight > 200) {
                        if (onSoftKeyBoardChangeListener != null) {
                            onSoftKeyBoardChangeListener.keyBoardHide(visibleHeight - rootViewVisibleHeight);
                        }
                        rootViewVisibleHeight = visibleHeight;
                        return;
                    }
    
                }
            });
        }
    
        private void setOnSoftKeyBoardChangeListener(OnSoftKeyBoardChangeListener onSoftKeyBoardChangeListener) {
            this.onSoftKeyBoardChangeListener = onSoftKeyBoardChangeListener;
        }
    
        public interface OnSoftKeyBoardChangeListener {
            void keyBoardShow(int height);
    
            void keyBoardHide(int height);
        }
    
        public static void setListener(Activity activity, OnSoftKeyBoardChangeListener onSoftKeyBoardChangeListener) {
            SoftKeyBoardListener softKeyBoardListener = new SoftKeyBoardListener(activity);
            softKeyBoardListener.setOnSoftKeyBoardChangeListener(onSoftKeyBoardChangeListener);
        }
    }
    

    给系统键盘添加头布局

    View decorView = ShenCeActivity.this.getWindow().getDecorView();
    view = (LinearLayout) View.inflate(ShenCeActivity.this,R.layout.top_view,null);
    FrameLayout contentParent =
            (FrameLayout) decorView.findViewById(android.R.id.content);
    FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.WRAP_CONTENT);
    layoutParams.gravity = Gravity.BOTTOM;
    layoutParams.bottomMargin = height;
    contentParent.addView(view,layoutParams);
    

    方案二,设置界面可被系统键盘顶起

    <activity android:name=".MainActivity"
        android:windowSoftInputMode="stateHidden|adjustResize">
    
    contentParent .setFitsSystemWindows(true);
    layoutParams.gravity = Gravity.BOTTOM;
    点击EditText会将底部布局顶起来,不需要监听计算系统键盘高度,设置添加的布局位于底部即可
    
    【A】stateUnspecified:软键盘的状态并没有指定,系统将选择一个合适的状态或依赖于主题的设置
    【B】stateUnchanged:当这个activity出现时,软键盘将一直保持在上一个activity里的状态,无论是隐藏还是显示
    【C】stateHidden:用户选择activity时,软键盘总是被隐藏
    【D】stateAlwaysHidden:当该Activity主窗口获取焦点时,软键盘也总是被隐藏的
    【E】stateVisible:软键盘通常是可见的
    【F】stateAlwaysVisible:用户选择activity时,软键盘总是显示的状态
    【G】adjustUnspecified:默认设置,通常由系统自行决定是隐藏还是显示
    【H】adjustResize:该Activity总是调整屏幕的大小以便留出软键盘的空间
    【I】adjustPan:当前窗口的内容将自动移动以便当前焦点从不被键盘覆盖和用户能总是看到输入内容的部分
    

    相关文章

      网友评论

          本文标题:系统键盘监控

          本文链接:https://www.haomeiwen.com/subject/axymeltx.html