最近公司的项目跟 H5 交互比较多,在开发的过程中发现输入框输入内容的时候被软键盘遮挡住看不到输入的内容,本以为修改下 Activity 页面的属性就可以解决问题,然而在添加了 adjustResize 属性后问题依旧。查找一番资料发现这是 Google 留下来的坑,在全屏状态下页面会自动屏蔽掉 adjustResize 等属性,这些属性加了也没啥用。不过还是找到相关的解决方案,直接建个工具类,复制以下代码:

import android.app.Activity;
import android.graphics.Rect;
import android.os.Build;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.FrameLayout;

public class WebViewInputFix {

    private View mChildOfContent;
    private int mUsableHeightPrevious;
    private FrameLayout.LayoutParams mFrameLayoutParams;
    private int mContentHeight;
    private boolean mIsfirst = true;
    private int mStatusBarHeight;
    
    public static void assistActivity(Activity activity) {
        new WebViewInputFix(activity);
    }

    private WebViewInputFix(Activity activity) {
        //获取状态栏的高度
        int resourceId = activity.getResources().getIdentifier("status_bar_height", "dimen", "android");
        mStatusBarHeight = activity.getResources().getDimensionPixelSize(resourceId);
        FrameLayout content = (FrameLayout) activity.findViewById(android.R.id.content);
        mChildOfContent = content.getChildAt(0);
        //界面出现变动都会调用这个监听事件
        mChildOfContent.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            public void onGlobalLayout() {
                if (mIsfirst) {
                    mContentHeight = mChildOfContent.getHeight();//兼容华为等机型
                    mIsfirst = false;
                }
                possiblyResizeChildOfContent();
            }
        });
        mFrameLayoutParams = (FrameLayout.LayoutParams)
                mChildOfContent.getLayoutParams();
    }

    //重新调整跟布局的高度
    private void possiblyResizeChildOfContent() {
        int usableHeightNow = computeUsableHeight();
        //当前可见高度和上一次可见高度不一致 布局变动
        if (usableHeightNow != mUsableHeightPrevious) {
            //int usableHeightSansKeyboard2 = mChildOfContent.getHeight();//兼容华为等机型
            int usableHeightSansKeyboard = mChildOfContent.getRootView().getHeight();
            int heightDifference = usableHeightSansKeyboard - usableHeightNow;
            if (heightDifference > (usableHeightSansKeyboard / 4)) {
                // keyboard probably just became visible
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                    //frameLayoutParams.height = usableHeightSansKeyboard - heightDifference;
                    mFrameLayoutParams.height = usableHeightSansKeyboard - heightDifference + mStatusBarHeight;
                } else {
                    mFrameLayoutParams.height = usableHeightSansKeyboard - heightDifference;
                }
            } else {
                mFrameLayoutParams.height = mContentHeight;
            }
            mChildOfContent.requestLayout();
            mUsableHeightPrevious = usableHeightNow;
        }
    }

    /**
     * 计算mChildOfContent可见高度
     */
    private int computeUsableHeight() {
        Rect r = new Rect();
        mChildOfContent.getWindowVisibleDisplayFrame(r);
        return (r.bottom - r.top);
    }
}

最后在 ActivityonCreat() 里调用一下 assistActivity(this) 就可以了。

如果觉得我的文章对你有用,请随意赞赏