關於Android中軟鍵盤顯示隱藏的監聽判斷總結

xingstarx發表於2018-01-18

前言

很多時候我們需要知道軟鍵盤是否開啟和關閉,從而處理一些需求。之前在做shou app的時候,由於互動比較複雜,需要知道軟鍵盤是開啟或者關閉,決定是否讓視訊全屏,還是讓聊天室全屏,或者是正常顯示(上面是視訊,下面是聊天室)

ViewTreeObserver.OnGlobalLayoutListener

OnGlobalLayoutListener 是ViewTreeObserver的內部類,當一個檢視樹的佈局發生改變時,可以被ViewTreeObserver監聽到,這是一個註冊監聽檢視樹的觀察者(observer),在檢視樹的全域性事件改變時得到通知。ViewTreeObserver不能直接例項化,而是通過getViewTreeObserver()獲得。 以上是說明,摘抄自http://www.cnblogs.com/superle/p/4567400.html這篇文章,也就是說,通過這個api,我們可以知道view的佈局狀態變化

如何監聽軟鍵盤

KeyboardChangeListener github上有這樣的一個輪子,測試了下,的確有效,思路是,對android.R.id.content所對應的view設定監聽,監聽它的高度變化,計算比較,來確定軟體盤是否彈起或者收攏了。這種模式需要設定activity android:windowSoftInputMode="adjustResize"

full screen模式下如何監聽軟體盤

在full screen 狀態上,發現,以上的方案完全不起作用了。在https://pspdfkit.com/blog/2016/keyboard-handling-on-android/ 上找到了解決辦法。 大致上說的是full screen模式下,adjustResize不會生效了,window不會resize的。  getWindowVisibleDisplayFrame(Rect)這個api可以獲取到view所在的區域,通過它比較每次區域的變化,可以實現監聽軟鍵盤是否可見 完整程式碼為:


public class KeyBoardChangeListener implements ViewTreeObserver.OnGlobalLayoutListener {
    private static final String TAG = "KeyBoardChangeListener";
    // Threshold for minimal keyboard height.
    final int MIN_KEYBOARD_HEIGHT_PX = 150;
    private final Rect windowVisibleDisplayFrame = new Rect();
    // Top-level window decor view.
    private View decorView;
    private int lastVisibleDecorViewHeight;
    private KeyBoardListener keyBoardListener;

    KeyBoardChangeListener(Activity activity) {
        if (activity == null) {
            Log.e(TAG, "activity is null");
            return;
        }
        decorView = activity.getWindow().getDecorView();
        decorView.getViewTreeObserver().addOnGlobalLayoutListener(this);
    }

    public void setKeyBoardListener(KeyBoardListener keyBoardListener) {
        this.keyBoardListener = keyBoardListener;
    }

    @Override
    public void onGlobalLayout() {
        // Retrieve visible rectangle inside window.
        decorView.getWindowVisibleDisplayFrame(windowVisibleDisplayFrame);
        final int visibleDecorViewHeight = windowVisibleDisplayFrame.height();
        // Decide whether keyboard is visible from changing decor view height.
        if (lastVisibleDecorViewHeight != 0) {
            if (lastVisibleDecorViewHeight > visibleDecorViewHeight + MIN_KEYBOARD_HEIGHT_PX) {
                // Calculate current keyboard height (this includes also navigation bar height when in fullscreen mode).
                int currentKeyboardHeight = decorView.getHeight() - windowVisibleDisplayFrame.bottom;
                // Notify listener about keyboard being shown.
                if (keyBoardListener != null) {
                    keyBoardListener.onKeyboardChange(true, currentKeyboardHeight);
                }
            } else if (lastVisibleDecorViewHeight + MIN_KEYBOARD_HEIGHT_PX < visibleDecorViewHeight) {
                // Notify listener about keyboard being hidden.
                if (keyBoardListener != null) {
                    keyBoardListener.onKeyboardChange(false, 0);
                }
            }
        }
        // Save current decor view height for the next call.
        lastVisibleDecorViewHeight = visibleDecorViewHeight;
    }

    public interface KeyBoardListener {
        /**
         * call back
         *
         * @param isShow         true is show else hidden
         * @param keyboardHeight keyboard height
         */
        void onKeyboardChange(boolean isShow, int keyboardHeight);
    }
}

複製程式碼

相關參考

https://pspdfkit.com/blog/2016/keyboard-handling-on-android/ KeyboardChangeListener https://gist.github.com/xingstarx/7a535fdda432e7678b53c628099c96ab

相關文章