最近遇到了软键盘挡住输入框的问题,上网搜罗半天,找到了一篇针对这个问题讲的比较全(好)的文章,地址放在下面:https://www.diycode.cc/topics/383

自己的收获:

  1. 普通Activity(不带WebView),直接在manifest文件中对activity设置:android:windowSoftInputMode的值adjustPan或者adjustResize即可;
  2. 如果带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);    }}

 

 

更多相关文章

  1. android中的四种启动模式
  2. android sqlite语句
  3. Android 夜间模式系列笔记(一)AppCompatDelegate
  4. Android MVP模式
  5. Android MVP 模式
  6. Android的联通性---USB主机模式(一)

随机推荐

  1. Android 获取网络图片方法
  2. android 字符串转换成JSON对象
  3. Android开发:文本实现两端对齐
  4. ANDROID开发,如何更改android的project ta
  5. 在TextView上显示图片信息
  6. android 自定义返回按钮
  7. Android - Toast自定义显示时间,以及时长
  8. Android /system/build.prop 文件
  9. android surfaceView控制视频显示大小
  10. java 语音