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

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, "根布局")
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值