欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

点击EditText的时候出现被键盘遮挡 在不使用scrollView的情况

程序员文章站 2022-06-24 09:57:58
public class KeyboardPatch { private Activity activity; private View decorView; private View contentView; private EditText editText; /** * 构造函数 * * @param act 需要解决bug的activity * @param contentView 界面容器,acti....
public class KeyboardPatch {
    private Activity activity;
    private View decorView;
    private View contentView;
    private EditText editText;

    /**
     * 构造函数
     *
     * @param contentView 界面根布局,
     */
    public KeyboardPatch(Activity activity, View contentView) {
        this.activity = activity;
        this.decorView = activity.getWindow().getDecorView();
        this.contentView = contentView;
    }

    /**
     * 监听layout变化
     */
    public void enable() {
        activity.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN | WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        if (Build.VERSION.SDK_INT >= 19) {
            decorView.getViewTreeObserver().addOnGlobalLayoutListener(onGlobalLayoutListener);
        }
    }

    /**
     * 取消监听
     */
    public void disable() {
        activity.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN | WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
        if (Build.VERSION.SDK_INT >= 19) {
            decorView.getViewTreeObserver().removeOnGlobalLayoutListener(onGlobalLayoutListener);
        }
    }


    private ViewTreeObserver.OnGlobalLayoutListener onGlobalLayoutListener = new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            Rect r = new Rect();
            decorView.getWindowVisibleDisplayFrame(r);
            int height = decorView.getContext().getResources().getDisplayMetrics().heightPixels;
            int diff = height - r.bottom;
            if (diff > 100) {
                Log.d("Tag", "抬起键盘" + diff + "------screen=" + height);
                change(diff, height);
            } else {
                Log.d("Tag", "关闭键盘" + diff + "------screen=" + height);
                change(0, height);
            }
        }
    };

    public void setEditTextView(EditText editText) {
        this.editText = editText;
    }

    private void change(int diff, int screenHeight) {
        if (diff > 0) {
            int[] location = new int[2];
            editText.getLocationInWindow(location);
            int visiHeight = screenHeight - diff;//屏幕的高度——键盘的高度 ==可视高度
            int local = location[1];//editText的高度 top--y
            int editBottomHeight = local + editText.getHeight();//view的底部的高度
            if (editBottomHeight > visiHeight) {//如果底部的高度大于 可见的高度 说明遮挡 往上移动
                contentView.setPadding(0, visiHeight - editBottomHeight, 0, 0);
            }
        } else {
            contentView.setPadding(0, 0, 0, 0);
        }
    }

}

 

 

//使用

var keyboard: KeyboardPatch? = null

edit_.setOnFocusChangeListener { v, hasFocus ->
    if (hasFocus) {
        keyboard?.enable()
        keyboard?.setEditTextView(edit_)
    } else {
       keyboard?.disable()
    }

}

keyboard= KeyboardPatch(this, "根布局")

本文地址:https://blog.csdn.net/qq_34102914/article/details/110238595