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

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

相关推荐
mygljx1 小时前
【MySQL 的 ONLY_FULL_GROUP_BY 模式】
android·数据库·mysql
冬奇Lab3 小时前
AudioTrack音频播放流程深度解析
android·音视频开发·源码阅读
青莲8436 小时前
查找算法详解
android·前端
青莲8436 小时前
排序算法详解
android·前端
zd2005726 小时前
用摩斯密码「听」时间:一款安卓报时应用的诞生
android
不会写代码的猴子7 小时前
Android17版本更新预览
android·android studio
用户41659673693558 小时前
记一次深坑:RecyclerView + FlexboxLayoutManager 导致 canScrollVertically 误判的剖析与修复
android
Be for thing9 小时前
Android 音频硬件(Codec / 喇叭 / 麦克风)原理 + 功耗与问题定位实战(手机 / 手表通用)
android·学习·智能手机·音视频
吉哥机顶盒刷机9 小时前
S905L3A/L3AB芯片迎来安卓14新纪元:Sicha移植版固件深度评测与刷机指南
android·经验分享·刷机