2020-09-18 设置沉浸式状态栏,WebView底部输入框被键盘挡住问题

1.最近项目遇到了webView中使用输入框,但是输入框被遮挡的问题,于是上网百度了一下,大佬已经给出了解决办法。我只是在基础上做些适配调整,仅供记录!!!!!!!!!!!

public class AndroidBug5497Workaround {

public static void assistActivity(Activity activity) {

                    new AndroidBug5497Workaround(activity);

    }

   private Activityactivity;

    private ViewmChildOfContent;

    private int usableHeightPrevious;

    private FrameLayout.LayoutParamsframeLayoutParams;

    private AndroidBug5497Workaround(Activity activity) {

             this.activity = activity;

        FrameLayout content = (FrameLayout) activity.findViewById(android.R.id.content);

        mChildOfContent = content.getChildAt(0);

        mChildOfContent.getViewTreeObserver().addOnGlobalLayoutListener(new                   ViewTreeObserver.OnGlobalLayoutListener() {

public void onGlobalLayout() {

possiblyResizeChildOfContent();

            }

});

        frameLayoutParams = (FrameLayout.LayoutParams)mChildOfContent.getLayoutParams();

    }

private void possiblyResizeChildOfContent() {

int usableHeightNow = computeUsableHeight();

        if (usableHeightNow !=usableHeightPrevious) {

Rect frame =new Rect();

            int usableHeightSansKeyboard =mChildOfContent.getRootView().getHeight();

            //这个判断是为了解决19之前的版本不支持沉浸式状态栏导致布局显示不完全的问题

            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {

activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);

                int statusBarHeight = frame.top;

                usableHeightSansKeyboard -= statusBarHeight;

            }

int heightDifference = usableHeightSansKeyboard - usableHeightNow;

            if (heightDifference > (usableHeightSansKeyboard /4)) {

// keyboard probably just became visible

                frameLayoutParams.height = usableHeightSansKeyboard - heightDifference - (BarUtils.isNavBarVisible(activity) ? BarUtils.getNavBarHeight() :0);

            }else {

// keyboard probably just became hidden

                frameLayoutParams.height = usableHeightSansKeyboard - (BarUtils.isNavBarVisible(activity) ? BarUtils.getNavBarHeight() :0);

            }

mChildOfContent.requestLayout();

            usableHeightPrevious = usableHeightNow;

        }

}

@SuppressLint("ObsoleteSdkInt")

private int computeUsableHeight() {

Rect frame =new Rect();

        activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);

        int statusBarHeight = frame.top;

        Rect r =new Rect();

        mChildOfContent.getWindowVisibleDisplayFrame(r);

        //这个判断是为了解决19之后的版本在弹出软键盘时,键盘和推上去的布局(adjustResize)之间有黑色区域的问题

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {

return (r.bottom - r.top) + statusBarHeight;

        }

return (r.bottom - r.top);

    }

}


2.解决问题,处理手机导航栏被遮挡