彻底解决软键盘遮挡DialogFragment

问题描述

点击edittext,弹出软键盘,dialog略微上移,edittext和下面的按钮还是被遮挡,上移的部分被切割

解决方案

设置dialog的inputMode,取消软键盘弹出自动上移
监听软键盘弹出事件,动态设置dialog的paddingBottom

override fun init() {
        dialog?.window?.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_NOTHING)
        globalListener = KeyboardUtils.registerSoftInputChangedListener(activity, object : KeyboardUtils.OnSoftInputChangedListener {
            override fun onSoftInputChanged(height: Int) {
                val activityPos = intArrayOf(0, 0)
                    val dialogPos = intArrayOf(0, 0)
                    activity.window.decorView.getLocationOnScreen(activityPos)
                    getLocationOnScreen(dialogPos)
                    val gap = activity.window.decorView.bottom + activityPos[1] - (bottom + dialogPos[1])
                    if (height > 10 && gap < height) {
                        dialog?.window?.decorView?.setPadding(0, 0, 0, height - gap + 50)
                    } else {
                        dialog?.window?.decorView?.setPadding(0, 0, 0, 0)
                    }
            }
        })
    }

override fun onDestroy() {
        super.onDestroy()
        if (globalListener != null) {
            KeyboardUtils.unregisterSoftInputChangedListener(activity, globalListener!!)
        }
    }

fun registerSoftInputChangedListener(activity: Activity,
                                         listener: OnSoftInputChangedListener?)
            : ViewTreeObserver.OnGlobalLayoutListener {
        val contentView = activity.findViewById<View>(android.R.id.content)
        sContentViewInvisibleHeightPre = getContentViewInvisibleHeight(activity)
        val globalListener = ViewTreeObserver.OnGlobalLayoutListener {
            if (listener != null) {
                val height = getContentViewInvisibleHeight(activity)
                if (sContentViewInvisibleHeightPre != height) {
                    listener.onSoftInputChanged(height)
                    sContentViewInvisibleHeightPre = height
                }
            }
        }
        contentView.viewTreeObserver.addOnGlobalLayoutListener(globalListener)
        return globalListener
    }

    fun unregisterSoftInputChangedListener(activity: Activity,
                                           listener: ViewTreeObserver.OnGlobalLayoutListener) {
        val contentView = activity.findViewById<View>(android.R.id.content)
        contentView.viewTreeObserver.removeOnGlobalLayoutListener(listener)
    }

private fun getContentViewInvisibleHeight(activity: Activity): Int {
        val contentView = activity.findViewById<View>(android.R.id.content)
        val outRect = Rect()
        contentView.getWindowVisibleDisplayFrame(outRect)
        return contentView.bottom - outRect.bottom
    }
总结

dialog销毁一定要移除global监听,否则回调里面拿到的window是上一次的

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值