AndroidBug5497Workaround 修改

来源:互联网 发布:ff14萨菲罗斯捏脸数据 编辑:程序博客网 时间:2024/06/06 10:48

公司要使用app,所以学习了一下android app的开发。
目的就是把html内容给展示出来而已,使用webview来呈现html内容。
可是,在点击输入框时,软键盘会遮挡输入框的内容。

可是自己无论怎么调都不成功,如果网页放入chrome浏览器中又可以。

去网上找了好多教程,说是android的一个bug,历史还很久远google还不想解决,所以得自己解决。

教程说让加载AndroidBug5497Workaround类,
在webview所在的activity的
onCreate方法里的
setContentView()后调用AndroidBug5497Workaround.assistActivity(this);这个方法。

调用了之后,是可以看见输入框了,但是又出现了一个问题。

因为html太长了,在webview中会滚动,但是调用了AndroidBug5497Workaround.assistActivity(this);这个方法之后不能出现滚动了。

滚动的原因是webview的高度太短,装不下html,所以才出现滚动。
看了这个类,他的功能就是监听键盘弹起回收的事件,通过这两个事件来影响webview的高度。

frameLayoutParams.height = usableHeightSansKeyboard - heightDifference;

这段代码是键盘弹起后设置的webview的高度,具体这个方法是干什么的我不知道,我就知道我的webview的高度会通过这个函数变短。

问题出在变回来的时候的高度上。
frameLayoutParams.height = usableHeightSansKeyboard;
就是上面这个高度,他会变成手机屏幕分辨率的高度Nexus 5X是1920

但是webview没那么高。所以我首先获取了这个webview原始的高度,等软键盘弹回来的时候,设置会webview原始的高度即可。

设置变量用来存储webview的初始高度
private int initheight;

因为初始化我获得的webview的高度为0,所以我直接把下面这段代码仍在监听方法里面
int tempheight=mChildOfContent.getHeight();
if(initheight<tempheight)
{
initheight=tempheight;
}
因为初始化的时候initheight为0,所以一开始监听,mChildOfContent.getHeight();所获得的就是webview的高,设置之后,在软键盘回收的代码中替换为
//frameLayoutParams.height = usableHeightSansKeyboard;
frameLayoutParams.height = initheight;

就大功告成了
修改后的AndroidBug5497Workaround

public class AndroidBug5497Workaround {    public static void assistActivity(Activity activity) {        new AndroidBug5497Workaround(activity);    }    private View mChildOfContent;    private int usableHeightPrevious;    private FrameLayout.LayoutParams frameLayoutParams;    private int initheight;    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 void possiblyResizeChildOfContent() {        int tempheight=mChildOfContent.getHeight();        if(initheight<tempheight)        {            initheight=tempheight;        }        int usableHeightNow = computeUsableHeight();        if (usableHeightNow != usableHeightPrevious) {            int usableHeightSansKeyboard = mChildOfContent.getRootView().getHeight();            int heightDifference = usableHeightSansKeyboard - usableHeightNow;            if (heightDifference > (usableHeightSansKeyboard/4 )) {                frameLayoutParams.height = usableHeightSansKeyboard - heightDifference;            } else {                frameLayoutParams.height = initheight;            }            mChildOfContent.requestLayout();            usableHeightPrevious = usableHeightNow;        }    }    private int computeUsableHeight() {        Rect r = new Rect();        mChildOfContent.getWindowVisibleDisplayFrame(r);        return (r.bottom-r.top);    }}