1. 程式人生 > 其它 >Android:WebView內輸入框被鍵盤遮擋解決方案

Android:WebView內輸入框被鍵盤遮擋解決方案

技術標籤:Android UIandroid

先貼程式碼

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

public class WebWorkaround {

    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 WebWorkaround(activity); } private WebWorkaround(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()方法呼叫

WebWorkaround.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);