1. 程式人生 > >軟鍵盤擋住WebView中輸入框解決方法

軟鍵盤擋住WebView中輸入框解決方法

如果是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);
    }

兩種方法都很簡單,結果也不再擋住輸入框了。