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

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

相关推荐
古月-一个C++方向的小白1 小时前
MySQL数据库——数据类型
android·数据库·mysql
张小潇2 小时前
AOSP15 WMS/AMS系统开发 - WindowManagerService finishDraw与prepareSurface流程详解
android
帅次5 小时前
Modifier 链与顺序、测量与命中区域
android·kotlin·compose·modifier
leory5 小时前
请详细描述Handler消息机制的工作原理
android·面试
leory5 小时前
请描述Binder IPC的基本原理和工作流程
android·面试
leory5 小时前
View的事件分发机制是怎样的?dispatchTouchEvent、onInterceptTouchEvent、onTouchEvent的关系?
android·面试
zander2585 小时前
Canal本地部署保姆级教程
android
小仙女喂得猪7 小时前
2026 Android 组件化项目的AICoding落地实践
android·kotlin·ai编程
leory7 小时前
请详细描述JVM的垃圾回收机制?
android·面试
leory7 小时前
volatile关键字的作用是什么?它能保证原子性吗?
android·面试