软键盘遮挡住输入 解决方案
程序员文章站
2022-04-20 09:03:24
...
1、app不是全屏显示的情况
只需要在AndroidManifest文件中对activity设置:android:windowSoftInputMode
的值adjustPan
或者adjustResize
即可,像这样:
<activity
android:name=".DemoActivity"
android:windowSoftInputMode="adjustPan" >
...
</activity>
2、app全屏显示的时候,以上方法将不起效果,需要用 AndroidBug5497Workaround 这个类去处理
public class AndroidBug5497Workaround {
// For more information, see https://issuetracker.google.com/issues/36911528
// 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);
}
}
调用方法,在onCreate 中加入以下方法
AndroidBug5497Workaround.assistActivity(this);
ps:使用过程中有遇到webview的网页弹出层无效(软键盘弹出时界面没有上移)。后来发现弹出层的样式用的是position:fixed,把这个改成position:absolute,就可以了。
推荐阅读
-
Android 软键盘状态并隐藏输入法的实例
-
iOS项目开发键盘弹出遮挡输入框问题解决方案
-
Android 软键盘状态并隐藏输入法的实例
-
Android Studio 升级到3.0后输入法中文状态下无法选词的终极解决方案
-
Linux 下 Sublime Text 中文输入问题的解决方案
-
Android中自定义的dialog中的EditText无法弹出输入法解决方案
-
Android Studio 3.x版本 的输入法遇到的坑及解决方案
-
Android Studio 升级到3.0后输入法中文状态下无法选词的终极解决方案
-
输入法中软键盘中的特殊字符集合
-
Ubuntu下Sublime Text无法输入中文最简单的解决方案