当前位置: 首页> 财经> 金融 > android 嵌套webview,软键盘遮挡输入框

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

时间:2025/7/10 10:10:51来源:https://blog.csdn.net/sccd2009/article/details/140395560 浏览次数:0次

实际项目中,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() {@Overridepublic 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);}}

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

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

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: