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

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

### Webview 中输入法键盘遮挡输入框解决方案 在 Android 开发中,`WebView` 的输入框可能会因为软键盘弹出而被遮挡。这种情况下,可以通过调整 `AndroidManifest.xml` 文件中的配置以及优化布局来解决问题。 #### 方案一:修改 `windowSoftInputMode` 通过设置 `Activity` 的 `android:windowSoftInputMode` 属性可以有效解决软键盘遮挡问题。以下是两种常见的模式: - **`adjustResize`**: 当软键盘显示时,系统会自动调整页面大小以适应屏幕空间[^3]。 - **`adjustPan`**: 页面不会重新计算尺寸,而是滚动到当前聚焦的输入框位置[^3]。 可以在 `AndroidManifest.xml` 中这样配置: ```xml <activity android:name=".YourWebViewActivity" android:windowSoftInputMode="adjustResize"> </activity> ``` 如果希望避免页面内容被压缩,则可以选择 `adjustPan` 模式: ```xml <activity android:name=".YourWebViewActivity" android:windowSoftInputMode="adjustPan"> </activity> ``` #### 方案二:动态调整视图高度 对于某些复杂场景,仅靠 `windowSoftInputMode` 可能不足以完全解决问题。此时可通过监听软键盘的状态并手动调整 `WebView` 或其父容器的高度。例如,在软键盘弹出时缩小 `WebView` 容器的高度,使其不覆盖输入框。 实现方法如下: ```java // 动态检测软键盘状态 View activityRootView = findViewById(R.id.activity_root); activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(() -> { int heightDiff = activityRootView.getRootView().getHeight() - activityRootView.getHeight(); if (heightDiff > dpToPx(200)) { // 如果高度差大于一定阈值(假设为200dp) // 调整 WebView 或其他控件的位置/高度 } else { // 还原默认布局 } }); private int dpToPx(int dp) { float density = getResources().getDisplayMetrics().density; return Math.round((float) dp * density); } ``` 此代码片段用于实时监测软键盘是否弹出,并根据情况执行自定义逻辑[^1]。 #### 方案三:使用 `ScrollView` 包裹 `WebView` 有时直接嵌套 `ScrollView` 并配合 `adjustResize` 使用也能达到良好效果。需要注意的是,这种方式可能会影响性能,因此需谨慎评估适用性。 示例 XML 布局: ```xml <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <!-- 其他控件 --> <WebView android:id="@+id/web_view" android:layout_width="match_parent" android:layout_height="wrap_content"/> </LinearLayout> </ScrollView> ``` #### 注意事项 尽管上述方法大多能够满足需求,但在实际开发过程中仍可能存在特殊情况。例如,部分设备或 ROM 对于软键盘行为的支持存在差异,这可能导致某些方案失效。在这种情况下,建议尝试多种组合策略,甚至考虑针对特定机型做兼容处理[^2]。 --- 问题
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值