欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

软键盘挡住WebView中输入框解决方法

程序员文章站 2022-04-20 09:00:42
...

如果是EditText,弹出软键盘好处理,会自动把输入框顶上来。

但如果输入框实在WebView中,那就不好处理了。

什么设置android:windowSoftInputMode属性啊,什么套一层ScrollView布局之类的都无效,最后再这里找到了终极解决方法,测试过有效。

附上链接:https://www.jianshu.com/p/306482e17080,感谢博主分享。

里面已经描述听详细的了,这里我也把该方法记录下来。

其实这是Android本身的一个issue 5497的bug。


解决方法有两种:

一、不用全屏模式,包含WebView的Activity使用了全屏模式,比如说没有标题栏之类的,就会出现这种遮挡的问题,如果可以的话去掉全屏就没问题了。


二、一定要全屏的Activity,那么也很简单,借助一个填坑类就可以了:

1、填坑的类AndroidBug5497Workaround:

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

/**
 * 解决WebView中输入时弹出软键盘,挡住输入框的问题
 * copy from https://www.jianshu.com/p/306482e17080
 * Created by ldw on 2018/5/23.
 */

public class AndroidBug5497Workaround {

    // For more information, see https://code.google.com/p/android/issues/detail?id=5497
    // To use this class, simply invoke assistActivity() on an Activity that already has its content view set.

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

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

    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 usableHeightNow = computeUsableHeight();
        if (usableHeightNow != usableHeightPrevious) {
            int usableHeightSansKeyboard = mChildOfContent.getRootView().getHeight();
            int heightDifference = usableHeightSansKeyboard - usableHeightNow;
            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;
        }
    }

    private int computeUsableHeight() {
        Rect r = new Rect();
        mChildOfContent.getWindowVisibleDisplayFrame(r);
        return (r.bottom - r.top);// 全屏模式下: return r.bottom
    }

}

2、Activity中使用也很简单:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_practise);
        AndroidBug5497Workaround.assistActivity(this);
    }

两种方法都很简单,结果也不再挡住输入框了。