解决webView去掉状态栏 软键盘盖住输入框问题

如果项目添加如下代码取消状态栏,webview软键盘会遮住输入框:

        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

新建如下类:

package com.youli.newpaths;

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

/**
 * Created by liutao on 2018/12/28.
 */

public class AndroidBug5497Workaround {

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

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

    private AndroidBug5497Workaround(Activity activity) {

        //拿到当前XML文件的根布局
        FrameLayout content = (FrameLayout) activity.findViewById(android.R.id.content);

        //监听当前View的状态,进行通知回调,即"软键盘弹出""
        mChildOfContent = content.getChildAt(0);
        mChildOfContent.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            public void onGlobalLayout() {
                possiblyResizeChildOfContent();
            }
        });
        frameLayoutParams = (FrameLayout.LayoutParams) mChildOfContent.getLayoutParams();
    }

    /**
     * 重新设置高度
     * <p>
     * 把界面高度设置为可用高度
     */
    private void possiblyResizeChildOfContent() {
        int usableHeightNow = computeUsableHeight();
        if (usableHeightNow != usableHeightPrevious) {
            // int usableHeightSansKeyboard = activity.getWindowManager().getDefaultDisplay().getHeight();//获取屏幕尺寸,不包括虚拟功能高度 用这个可以完美解决
            //findViewById(android.R.id.content).getMeasuredHeight() 也可以解决虚拟按键问题
            int usableHeightSansKeyboard = mChildOfContent.getRootView().getHeight();
            int heightDifference = usableHeightSansKeyboard - usableHeightNow;
            //排除其他View引起的变化,专注软键盘变化
            if (heightDifference > (usableHeightSansKeyboard / 4)) {
                // keyboard probably just became visible
                frameLayoutParams.height = usableHeightSansKeyboard - heightDifference;
            } else {
                // keyboard probably just became hidden
                frameLayoutParams.height = usableHeightSansKeyboard;
            }
            mChildOfContent.requestLayout();
            usableHeightPrevious = usableHeightNow;
        }
    }

    /**
     * 软键盘弹出后,可以显示内容的高度
     *
     * @return
     */
    private int computeUsableHeight() {
        Rect r = new Rect();
        //这行代码能够获取到去除标题栏和被软键盘挡住的部分,所剩下的矩形区域
        mChildOfContent.getWindowVisibleDisplayFrame(r);
        //r.top : 标题栏的高度
        //屏幕高度-r.bottom : 软键盘的高度
        //可用高度(全屏模式) : rect.bottom
        //可用高度(非全屏模式) : rect.bottom - rect.top
        return (r.bottom - r.top);// 全屏模式下: return r.bottom
    }

}

在onCreate里面加上就可以解决:

   AndroidBug5497Workaround.assistActivity(this);

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值