android 软件盘弹起 隐藏 以及高度的计算

android 软件盘弹起 隐藏 以及高度的计算

public class AndroidBug5497Workaround {
    
    

    public interface SoftKeyboardListener {
    
    
        void onSoftKeyboardShow(int currentBottom, int previousBottom);

        void onSoftKeyboardHide(int currentBottom, int previousBottom);
    }

    public static void assistActivity(Activity activity, SoftKeyboardListener lis) {
    
    
        new AndroidBug5497Workaround(activity, lis);
    }

    private View mContentView;       //activity的contentview
    private volatile int mBottomPrevious;    //可用区域的bottom
    private SoftKeyboardListener mKeyBoardListener;  //软键盘显示隐藏listener

    private AndroidBug5497Workaround(Activity activity, SoftKeyboardListener lis) {
    
    
        mKeyBoardListener = lis;
        FrameLayout content = (FrameLayout) activity.findViewById(android.R.id.content);
        mContentView = content.getChildAt(0);
        mBottomPrevious = getAvailableAreaBottom();
        mContentView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
    
    
            public void onGlobalLayout() {
    
    
                possiblyResizeChildOfContent();
            }
        });
    }

    private synchronized void possiblyResizeChildOfContent() {
    
    
        final int availableAreaBottom = getAvailableAreaBottom();
        if (availableAreaBottom != mBottomPrevious) {
    
    
            if (mBottomPrevious != 0) {
    
    
                int displayHeight = mContentView.getRootView().getHeight();
                int bottomDiff = availableAreaBottom - mBottomPrevious;
                if (bottomDiff > displayHeight / 4) {
    
    
                    mKeyBoardListener.onSoftKeyboardHide(availableAreaBottom, mBottomPrevious);
                } else if (-bottomDiff > displayHeight / 4) {
    
    
                    mKeyBoardListener.onSoftKeyboardShow(availableAreaBottom, mBottomPrevious);
                }
            }

            mBottomPrevious = availableAreaBottom;
        }
    }

    private int getAvailableAreaBottom() {
    
    
        Rect r = new Rect();
        mContentView.getWindowVisibleDisplayFrame(r);
        return r.bottom;
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_38687303/article/details/125313430