【Android】軟鍵盤彈出收起事件監聽

在開發過程中有時候會有監聽軟鍵盤彈出收起事件的需求,在此作記錄,以便以後再次遇到,少走彎路。

彈出和隱藏軟鍵盤方法:

 /**
     * 彈出軟鍵盤
     */
    public void showKeyBoard(View v) {
        InputMethodManager imm = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
        if(null != imm) {
            v.requestFocus();
            imm.showSoftInput(v, 0);
        }
        isKeyboardShow = true;
    }

 /**
     * 隱藏軟鍵盤
     */
    public void hideKeyBoard(View v) {
        InputMethodManager imm = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
        if(null != imm) {
            v.requestFocus();
            imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
        }
        isKeyboardShow = false;
    }

基於以上的彈出收起操作,給出監聽實體類:

/**
 * Created by Luzj on 2018/8/20.
 *
 * 軟鍵盤彈出收起監聽
 */
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);

                int visibleHeight = r.height();
                System.out.println(""+visibleHeight);
                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);
    }
}

在Activity中註冊監聽:

SoftKeyBoardListener.setListener(this, onSoftKeyBoardChangeListener);

聲明監聽實例:

/**
     * 軟鍵盤彈出收起監聽
     */
    private SoftKeyBoardListener.OnSoftKeyBoardChangeListener onSoftKeyBoardChangeListener = new SoftKeyBoardListener.OnSoftKeyBoardChangeListener() {
        @Override
        public void keyBoardShow(int height) {
            show("鍵盤顯示 高度 : " + height);
        }
        @Override
        public void keyBoardHide(int height) {
            show("鍵盤隱藏 高度 : " + height);
        }
    };

 ps:

有時候彈出軟鍵盤會使佈局出現出乎預期的現象,這時候要考慮Manifest文件中Activity的windowSoftInputMode屬性。

在我的開發中,想做軟鍵盤彈出時,部分屏幕出現蒙層的結果,但是在軟鍵盤彈出時蒙層總會自動消失,這時候在Manifest的相應Activity中加上:

android:windowSoftInputMode="adjustPan"

 結果正常。

總結:軟鍵盤導致的佈局問題,往往在windowSoftInputMode屬性中可以找到答案。

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章