天天看點

軟鍵盤的彈出隐藏監聽及軟鍵盤高度軟鍵盤的彈出隐藏監聽及軟鍵盤高度

軟鍵盤的彈出隐藏監聽及軟鍵盤高度

網上找過很多基本都能做到,就是通過監聽根視圖高度的變化來判斷鍵盤的彈出和隐藏,但是會出現一個問題,就是當鍵盤從拼音切換成手寫時軟鍵盤依舊是彈出狀态,但是高度發生變化,導緻監聽出現問題。

-直接上代碼

public class SoftKeyBoardListener {
    private View rootView;//activity的根視圖
    int rootViewVisibleHeight = 0;//記錄根視圖的顯示高度
    int rootViewHeight = 0;     //記錄根視圖正常顯示高度
    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);
                int visibleHeight = r.height();
                if (rootViewHeight == 0) {
                    rootViewHeight = visibleHeight;
                }
                if (rootViewVisibleHeight == 0) {
                    rootViewVisibleHeight = visibleHeight;
                    return;
                }

                //根視圖顯示高度沒有變化,可以看作軟鍵盤顯示/隐藏狀态沒有改變
                if (rootViewVisibleHeight == visibleHeight) {
                    return;
                }

                //根視圖顯示高度變小超過200,可以看作軟鍵盤顯示了
                if (rootViewHeight - visibleHeight >= 200) {
                    if (onSoftKeyBoardChangeListener != null) {
                        onSoftKeyBoardChangeListener.keyBoardShow(rootViewHeight - visibleHeight);
                    }
                    rootViewVisibleHeight = visibleHeight;
                    return;
                }

                //根視圖顯示高度變大超過200,同時鍵盤顯示高度應該等于實際高度可以看作軟鍵盤隐藏了
                if (visibleHeight - rootViewVisibleHeight > 200 && rootViewHeight == visibleHeight) {
                    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);
    }
}           

-使用方法

SoftKeyBoardListener.setListener(MainActivity.this, new SoftKeyBoardListener
                .OnSoftKeyBoardChangeListener() {
            @Override
            public void keyBoardShow(int height) {
                System.out.println("鍵盤顯示" + height);
            }

            @Override
            public void keyBoardHide(int height) {
                System.out.println("鍵盤隐藏" + height);
            }
        });