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);
        }
    }

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

相关推荐
Dingdangr5 小时前
Android中的Intent的作用
android
技术无疆5 小时前
快速开发与维护:探索 AndroidAnnotations
android·java·android studio·android-studio·androidx·代码注入
GEEKVIP5 小时前
Android 恢复挑战和解决方案:如何从 Android 设备恢复删除的文件
android·笔记·安全·macos·智能手机·电脑·笔记本电脑
Jouzzy12 小时前
【Android安全】Ubuntu 16.04安装GDB和GEF
android·ubuntu·gdb
极客先躯12 小时前
java和kotlin 可以同时运行吗
android·java·开发语言·kotlin·同时运行
Good_tea_h15 小时前
Android中的单例模式
android·单例模式
计算机源码社19 小时前
分享一个基于微信小程序的居家养老服务小程序 养老服务预约安卓app uniapp(源码、调试、LW、开题、PPT)
android·微信小程序·uni-app·毕业设计项目·毕业设计源码·计算机课程设计·计算机毕业设计开题
丶白泽20 小时前
重修设计模式-结构型-门面模式
android
晨春计21 小时前
【git】
android·linux·git
标标大人1 天前
c语言中的局部跳转以及全局跳转
android·c语言·开发语言