Android webview input标签 软键盘遮挡问题

在App中使用Webview中的网页使用了input标签,用于选择文件并且上传;

问题:

点击input标签 后软键盘出现,但是整体布局未向上移动,造成软键盘遮挡put标签等。

解决方案:

参看:stackoverflow 问题

简单来说就是使用AndroidBug5497Workaround 类,在onCreate时设置

虚拟按键出现问题

设备:红米redmi 5 plus

类似红米5 plus 这样的设备底部可能有虚拟按键,并且加载的网页十分特殊,网页底部有一个pop,如图

image

在使用AndroidBug5497Workaround 出现pop(红框部分)被底部的虚拟键(蓝框部分)遮挡。

解决方案

 * 为修复WebView内的input 弹出软键盘导致布局被遮挡引入
 * https://stackoverflow.com/questions/7417123/android-how-to-adjust-layout-in-full-screen-mode-when-softkeyboard-is-visible/19494006#19494006
 */
public class AndroidBug5497Workaround {
    // For more information, see https://issuetracker.google.com/issues/36911528
    // To use this class, simply invoke assistActivity() on an Activity that already has its content view set.

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

    private View mChildOfContent;
    private int usableHeightPrevious;
    private FrameLayout.LayoutParams frameLayoutParams;

    private AndroidBug5497Workaround(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 int frameLayoutHeight = 0;

    private void possiblyResizeChildOfContent() {
        int usableHeightNow = computeUsableHeight();
        if (usableHeightNow != usableHeightPrevious) {
            int usableHeightSansKeyboard = mChildOfContent.getRootView().getHeight();
            int heightDifference = usableHeightSansKeyboard - usableHeightNow;
            if (heightDifference > (usableHeightSansKeyboard/4)) {
                // keyboard probably just became visible
                frameLayoutHeight = frameLayoutParams.height;// !!!修改前保存原有高度
                frameLayoutParams.height = usableHeightSansKeyboard - heightDifference;
            } else {
                // keyboard probably just became hidden
                if(0 != frameLayoutHeight) {
                    frameLayoutParams.height = frameLayoutHeight;// !!!收起键盘恢复原有高度
                }
            }
            mChildOfContent.requestLayout();
            usableHeightPrevious = usableHeightNow;
        }
    }

    private int computeUsableHeight() {
        Rect r = new Rect();
        mChildOfContent.getWindowVisibleDisplayFrame(r);
        return (r.bottom - r.top);
    }
}```

你可能感兴趣的:(Android webview input标签 软键盘遮挡问题)