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

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

(非全屏模式,多个输入框可用:https://blog.csdn.net/smiletofuture/article/details/106403452

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

参考2:https://blog.csdn.net/woqq863787405/article/details/81170145 点击软键盘外区域隐藏软键盘

参考3: https://blog.csdn.net/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方法如下  https://www.jb51.net/article/137229.htm
     *
     * @param main   根布局
     * @param scroll 需要显示的最下方View
     */
    public static void addLayoutListener(final View main, final EditText scroll) {
        isSoftKeyboardShowing = false;
        mOnGlobalLayoutListener = new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public 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);
        }
//        原文链接:https://blog.csdn.net/ccpat/article/details/46730771
    }

    //判断软键盘是否正在展示
    public static boolean isSoftShowing(View decorView) {
        //获取当前屏幕内容的高度
        int screenHeight = decorView.getHeight();
        //获取View可见区域的bottom
        Rect 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之外的区域隐藏键盘
    @Override
    public 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);
    }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值