Android软键盘挡住输入框
16lz
2021-01-23
最近遇到了软键盘挡住输入框的问题,上网搜罗半天,找到了一篇针对这个问题讲的比较全(好)的文章,地址放在下面:https://www.diycode.cc/topics/383
自己的收获:
- 普通Activity(不带WebView),直接在manifest文件中对activity设置:
android:windowSoftInputMode
的值adjustPan
或者adjustResize
即可; - 如果带WebView:
- 如果非
全屏模式
,可以使用adjustResize;
- 如果是
全屏模式
,则使用AndroidBug5497Workaround
进行处理。
- 如果非
注:
1、这里的全屏模式
即是页面是全屏的,包括Application或activity使用了Fullscreen主题、使用了『状态色着色』、『沉浸式状态栏』、『Immersive Mode』等等——总之,基本上只要是App自己接管了状态栏的控制,就会产生这种问题。
2、AndroidBug5497Workaround的使用:
- 把
AndroidBug5497Workaround
类复制到项目中; - 在需要填坑的activity的setContentView()语句后,添加一句
AndroidBug5497Workaround.assistActivity(this)
即可。
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); }}
更多相关文章
- android中的四种启动模式
- android sqlite语句
- Android 夜间模式系列笔记(一)AppCompatDelegate
- Android MVP模式
- Android MVP 模式
- Android的联通性---USB主机模式(一)