1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > 全屏模式下软键盘弹出遮挡输入框问题

全屏模式下软键盘弹出遮挡输入框问题

时间:2023-02-07 14:48:37

相关推荐

全屏模式下软键盘弹出遮挡输入框问题

状态栏透明,全屏模式,一个页面单个输入框可用

(非全屏模式,多个输入框可用:/smiletofuture/article/details/106403452)

参考1:/article/137229.htm使用的该文章中方法五进行了处理

参考2:/woqq863787405/article/details/81170145点击软键盘外区域隐藏软键盘

参考3:/ccpat/article/details/46730771

软键盘弹出遮挡输入框的处理工具类:

import android.content.Context;import android.graphics.Rect;import android.os.Build;import android.view.MotionEvent;import android.view.View;import android.view.ViewTreeObserver;import android.view.inputmethod.InputMethodManager;import android.widget.EditText;public class SoftKeyBoardUtils {public static void hideKeyboard(View view){InputMethodManager imm = (InputMethodManager) view.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);if (imm != null) {imm.hideSoftInputFromWindow(view.getWindowToken(),0);}}private static ViewTreeObserver.OnGlobalLayoutListener mOnGlobalLayoutListener;private static boolean isSoftKeyboardShowing;//在Activity的onCreate里添加如下方法addLayoutListener(main,login_btn);/**/*** addLayoutListener方法如下 /article/137229.htm** @param main 根布局* @param scroll 需要显示的最下方View*/public static void addLayoutListener(final View main, final EditText scroll) {isSoftKeyboardShowing = false;mOnGlobalLayoutListener = new ViewTreeObserver.OnGlobalLayoutListener() {@Overridepublic void onGlobalLayout() {Rect rect = new Rect(); //1、获取main在窗体的可视区域main.getWindowVisibleDisplayFrame(rect);//2、获取main在窗体的不可视区域高度,在键盘没有弹起时,main.getRootView().getHeight()调节度应该和rect.bottom高度一样int screenHeight = main.getRootView().getHeight();//屏幕高度int mainInvisibleHeight = screenHeight - (rect.bottom - rect.top);//3、不可见区域大于屏幕本身高度的1/4:说明键盘弹起了boolean isKeyBoardShowing = mainInvisibleHeight > screenHeight / 4;if ((!isSoftKeyboardShowing && isKeyBoardShowing) || (isSoftKeyboardShowing && !isKeyBoardShowing)) {isSoftKeyboardShowing = true;int[] location = new int[2];scroll.getLocationInWindow(location); // 4、获取Scroll的窗体坐标,算出main需要滚动的高度int srollHeight = (location[1] + scroll.getHeight()) - rect.bottom; //5、让界面整体上移键盘的高度if (srollHeight < 0){srollHeight = 0;isSoftKeyboardShowing = false;}main.scrollTo(0, srollHeight);} else if(!isKeyBoardShowing){ //3、不可见区域小于屏幕高度1/4时,说明键盘隐藏了,把界面下移,移回到原有高度isSoftKeyboardShowing = false;main.scrollTo(0, 0);}}};main.getViewTreeObserver().addOnGlobalLayoutListener(mOnGlobalLayoutListener);}public static void removeGlobalLayoutListener(View decorview) {//移除布局变化监听if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {decorview.getViewTreeObserver().removeOnGlobalLayoutListener(mOnGlobalLayoutListener);} else {decorview.getViewTreeObserver().removeGlobalOnLayoutListener(mOnGlobalLayoutListener);}// 原文链接:/ccpat/article/details/46730771}//判断软键盘是否正在展示public static boolean isSoftShowing(View decorView) {//获取当前屏幕内容的高度int screenHeight = decorView.getHeight();//获取View可见区域的bottomRect rect = new Rect();decorView.getWindowVisibleDisplayFrame(rect);return screenHeight - rect.bottom != 0;}//是否需要隐藏键盘public static boolean isShouldHideInput(View v, MotionEvent event) {if (v != null && (v instanceof EditText)) {int[] leftTop = {0, 0};//获取输入框当前的location位置v.getLocationInWindow(leftTop);int left = leftTop[0];int top = leftTop[1];int bottom = top + v.getHeight();int right = left + v.getWidth();if (event.getX() > left && event.getX() < right&& event.getY() > top && event.getY() < bottom) {// 点击的是输入框区域,保留点击EditText的事件return false;} else {return true;}}return false;}}

简单调用,在需要处理的activity中加入以下代码(当前页面有两个EditText时,无法正常使用):

SoftKeyBoardUtils.addLayoutListener(getWindow().getDecorView(), findViewById(R.id.et_calc));

点击软键盘外区域隐藏软键盘,在需要处理的页面加入以下代码:

//点击EditText之外的区域隐藏键盘@Overridepublic boolean dispatchTouchEvent(MotionEvent ev) {if (ev.getAction() == MotionEvent.ACTION_DOWN) {View v = getCurrentFocus();if (SoftKeyBoardUtils.isSoftShowing(getWindow().getDecorView()) && SoftKeyBoardUtils.isShouldHideInput(v, ev)) {SoftKeyBoardUtils.hideKeyboard(getWindow().getDecorView());}}return super.dispatchTouchEvent(ev);}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。