Android内嵌H5页面呼出软键盘经常会遮挡输入框,最简单的解决方案是在AndroidManifest文件中对activity设置:android:windowSoftInputMode的值adjustPan或者adjustResize即可,像这样:
<activity
android:name=".MainActivity"
android:windowSoftInputMode="adjustPan" >
</activity>
如果这样解决不了,那么就需要利用AndroidBug5497Workaround的解决方案
1.将AndroidBug5497Workaround代码拷贝到项目中
2.在相应的Activity中的onCreate方法中添加一句AndroidBug5497Workaround.assistActivity(this)即可
AndroidBug5497Workaround类
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 - getNavigationBarHeight(mChildOfContent.getContext());
}
mChildOfContent.requestLayout();
usableHeightPrevious = usableHeightNow;
}
}
private int computeUsableHeight() {
Rect r = new Rect();
mChildOfContent.getWindowVisibleDisplayFrame(r);
return (r.bottom - r.top);
}
private int getNavigationBarHeight(Context context) {
boolean hasMenuKey = ViewConfiguration.get(context).hasPermanentMenuKey();
if (!hasMenuKey) {
// This device has a navigation bar
int resourceId = context.getResources().getIdentifier("navigation_bar_height", "dimen", "android");
if (checkNavigationBarShow(mChildOfContent.getContext()) && resourceId > 0) {
return context.getResources().getDimensionPixelSize(resourceId);
} else {
return 0;
}
}
return 0;
}
private boolean checkNavigationBarShow(@NonNull Context context) {
boolean show = false;
if (context instanceof Activity) {
Window window = ((Activity) context).getWindow();
Display display = window.getWindowManager().getDefaultDisplay();
Point point = new Point();
display.getRealSize(point);
View decorView = window.getDecorView();
Configuration conf = context.getResources().getConfiguration();
if (Configuration.ORIENTATION_LANDSCAPE == conf.orientation) {
View contentView = decorView.findViewById(android.R.id.content);
show = (point.x != contentView.getWidth());
} else {
Rect rect = new Rect();
decorView.getWindowVisibleDisplayFrame(rect);
show = (rect.bottom != point.y);
}
}
return show;
}
}