首页 Android 正文
  • 本文约2512字,阅读需13分钟
  • 306
  • 0

Android解决Webview键盘遮挡输入框问题

摘要

android 开发过程中,避免不了与webview 打交道,相信不少小伙伴,也遇到过,webview 输入框被输入法遮挡问题。 android Webview 输入法被遮挡。处理方案存在如下两种情况...

android 开发过程中,避免不了与webview 打交道,相信不少小伙伴,也遇到过,webview 输入框被输入法遮挡问题。

android Webview 输入法被遮挡。处理方案存在如下两种情况。一是activity 未设置SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN,二是activity设置SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN。

activity未设置SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN

此种情况下可以通过在清单文件设置窗口软键盘交互模式为android:windowSoftInputMode="adjustResize" 即可解决。如下所示:

 <activity
            android:name=".web.WebActivity"
            android:excludeFromRecents="false"
            android:windowSoftInputMode="adjustResize" />

activity 设置SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN时

解决方案为通过监听软键盘的弹出,计算软键盘的高度,动态调整webView 的margin 来达到目的。

软键盘高度计算

Webview对应的Activity的onCreate()方法中调用工具类: WebviewKeyboardUtils.assistActivity(this);

public class WebViewKeyboardUtils {

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

    private View mChildOfContent;
    private int usableHeightPrevious;
    private FrameLayout.LayoutParams frameLayoutParams;
    private int contentHeight;
    private boolean isfirst = true;
    private Activity activity;
    private int statusBarHeight;

    private WebViewKeyboardUtils(Activity activity) {
        //获取状态栏的高度
        int resourceId = activity.getResources().getIdentifier("status_bar_height", "dimen", "android");
        statusBarHeight = activity.getResources().getDimensionPixelSize(resourceId);
        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() {
                if (isfirst) {
                    contentHeight = mChildOfContent.getHeight();//兼容华为等机型
                    isfirst = false;
                }
                possiblyResizeChildOfContent();
            }
        });
        frameLayoutParams = (FrameLayout.LayoutParams)
                mChildOfContent.getLayoutParams();
    }

    //重新调整跟布局的高度
    private void possiblyResizeChildOfContent() {
        int usableHeightNow = computeUsableHeight();
        //当前可见高度和上一次可见高度不一致 布局变动
        if (usableHeightNow != usableHeightPrevious) {
            //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;
                    frameLayoutParams.height = usableHeightSansKeyboard - heightDifference + statusBarHeight;
                } else {
                    frameLayoutParams.height = usableHeightSansKeyboard - heightDifference;
                }
            } else {
                frameLayoutParams.height = contentHeight;
            }
            mChildOfContent.requestLayout();
            usableHeightPrevious = usableHeightNow;
        }
    }

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

扫描二维码,在手机上阅读


    评论