實際項目中,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);}}
歡迎轉發、點贊、收藏。---------一個奮斗前線的老碼農。