webview内输入框被键盘遮挡解决方案总结

首先贴出AndroidBug5497Workaround代码

package com.jtz.jingtaizhuang.utils;

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

public class AndroidBug5497Workaround {

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

    public static void assistActivity(Activity activity, int mheight) {
        /**
         * 这个传进来的mheight是tablayout控件的高度,如果因为tablayout被键盘弹起来,
         * 可以传入这个值对其进行操作
         */
        height = mheight;
        new AndroidBug5497Workaround(activity);
    }

    private AndroidBug5497Workaround(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 = activity;

        //界面出现变动都会调用这个监听事件
        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)) {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                    /**
                     * 以下代码便是对可见高度进行赋值
                     */
                    frameLayoutParams.height = usableHeightSansKeyboard
                            - heightDifference + statusBarHeight + height;
//                    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的活动的oncreate()方法调用

AndroidBug5497Workaround.assistActivity(this,height);

----------------------------------

对height的说明:

这里的高度可以是某个控件的高度,比如如果tablayout(底部菜单栏).测量tablayout的代码如下

int w = View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED);
int h = View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED);
tabLayout.measure(w,h);
int height = tabLayout.getMeasuredHeight();
int wight = tabLayout.getMeasuredWidth();
LogUtils.e("h:"+height+"w:"+wight);


阅读更多
文章标签: android
个人分类: Android开发
上一篇日期选择器参考地址
下一篇获取当前日期年月日时分
想对作者说点什么? 我来说一句

IOS 解决输入框键盘遮挡问题

2015年06月15日 65KB 下载

没有更多推荐了,返回首页

关闭
关闭