android 嵌套webview,软键盘遮挡输入框

实际项目中,android需要加载h5,经常遇到软键盘遮盖输入框的情况,h5测试的时候,是没问题的,但是在APP中是不能把页面推上去。经测试完美解决了这个问题。

1. oncreate

***************************
try {
            web();
            layoutParams = (ConstraintLayout.LayoutParams) webView.getLayoutParams();
            decorView = getActivity().getWindow().getDecorView();
            decorView = getActivity().getWindow().getDecorView();
            if (decorView != null) {
                globalLayoutListener = new ViewTreeObserver.OnGlobalLayoutListener() {
                    @Override
                    public void onGlobalLayout() {
                        if (webView != null) {
                            Rect rect = new Rect();
                            decorView.getWindowVisibleDisplayFrame(rect);
                            int screenHeight = decorView.getRootView().getHeight();
                            int keyboardHeight = screenHeight - rect.bottom;

                            if (currentHeight != keyboardHeight && keyboardHeight > 100) {
                                currentHeight = keyboardHeight;
                                ConstraintLayout.LayoutParams layoutParams = (ConstraintLayout.LayoutParams) webView.getLayoutParams();
                                if (layoutParams != null) {
                                    layoutParams.bottomMargin = keyboardHeight;
                                    webView.setLayoutParams(layoutParams);
                                }
                            }
                        }
                    }
                };

                ViewTreeObserver viewTreeObserver = decorView.getViewTreeObserver();
                if (viewTreeObserver != null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                    viewTreeObserver.addOnGlobalLayoutListener(globalLayoutListener);
                }
            }

        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
*****************************

2. 不要忘记

onDestroyView 销毁资源
public void onDestroyView() {
        super.onDestroyView();
        if (webView != null) {
            webView.stopLoading();
            webView.clearCache(true);
            webView.clearHistory();
            webView.destroy(); // 注意:调用destroy()后,WebView实例就不能再使用了
            webView = null;
        }
        if (decorView != null && decorView.getViewTreeObserver() != null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            decorView.getViewTreeObserver().removeOnGlobalLayoutListener(globalLayoutListener);
        }
    }

欢迎转发、点赞、收藏。---------一个奋斗前线的老码农。

相关推荐

  1. android 嵌套webview键盘遮挡输入

    2024-07-14 04:12:02       24 阅读
  2. Android 8.1 实体键盘输入时收起键盘

    2024-07-14 04:12:02       36 阅读
  3. Android 键盘的显示和隐藏

    2024-07-14 04:12:02       54 阅读
  4. 【HarmonyOS】输入焦点控制实现键盘显隐

    2024-07-14 04:12:02       31 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-14 04:12:02       67 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-14 04:12:02       72 阅读
  3. 在Django里面运行非项目文件

    2024-07-14 04:12:02       58 阅读
  4. Python语言-面向对象

    2024-07-14 04:12:02       69 阅读

热门阅读

  1. ref和reactive区别

    2024-07-14 04:12:02       21 阅读
  2. 【随想】闲聊、沟通和谈判

    2024-07-14 04:12:02       20 阅读
  3. nginx防盗链

    2024-07-14 04:12:02       26 阅读
  4. 【C++编程】类的静态 static 成员 & 常 const 函数

    2024-07-14 04:12:02       19 阅读
  5. Python自定义可切片的类

    2024-07-14 04:12:02       20 阅读
  6. 力扣题解(最长的斐波那契子序列的长度)

    2024-07-14 04:12:02       24 阅读
  7. Mojo: 轻量级Perl框架的魔力

    2024-07-14 04:12:02       21 阅读
  8. 最长上升子序列(最长递增子序列,LIS)

    2024-07-14 04:12:02       20 阅读
  9. 【docker镜像如何在不同的架构上运行】

    2024-07-14 04:12:02       19 阅读
  10. 第九十五周周报

    2024-07-14 04:12:02       14 阅读