美文网首页
adjustNothing时监听键盘状态及高度

adjustNothing时监听键盘状态及高度

作者: Liuzjdev | 来源:发表于2021-03-08 15:17 被阅读0次

背景

开发直播需求时,要求直播间内键盘弹起不压缩界面,也就是不能用windowSoftInputMode#adjustResize这个属性,因为他会压缩界面。要不压缩界面肯定就要用adjustNothing,也就是在这个模式下获取键盘弹起高度,下面就是具体实现。

效果图

Record_2021-03-08-13-54-49_8b18727de96c12142f9e23842beba4f0.gif

代码实现

/**
 * 获取键盘高度帮助类
 *
 * @author Liuzj
 * @date 2021/3/5
 */
class KeyboardHeightProvider(
    activity: FragmentActivity,
    parentView: View,
    listener: KeyboardHeightListener?,
    ignoreStatusBarHeight: Boolean = false
) : PopupWindow(activity) {
    interface KeyboardHeightListener {
        fun onKeyboardHeightChanged(
            keyboardHeight: Int,
            keyboardOpen: Boolean,
            isLandscape: Boolean
        )
    }

    init {
        val popupView = LinearLayout(activity)
        popupView.layoutParams =
            LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT
            )
        popupView.viewTreeObserver.addOnGlobalLayoutListener {
            val metrics = DisplayMetrics()
            activity.windowManager.defaultDisplay.getMetrics(metrics)
            val rect = Rect()
            popupView.getWindowVisibleDisplayFrame(rect)
            var keyboardHeight: Int = metrics.heightPixels - (rect.bottom - rect.top)
            val resourceID: Int =
                activity.resources.getIdentifier("status_bar_height", "dimen", "android")
            if (resourceID > 0 && !ignoreStatusBarHeight) {
                keyboardHeight -= activity.resources.getDimensionPixelSize(resourceID)
            }
            if (keyboardHeight < 100) {
                keyboardHeight = 0
            }
            //是否是横屏
            val isLandscape = metrics.widthPixels > metrics.heightPixels
            val keyboardOpen = keyboardHeight > 0
            listener?.onKeyboardHeightChanged(keyboardHeight, keyboardOpen, isLandscape)
        }
        contentView = popupView
        softInputMode =
            WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE or WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE
        inputMethodMode = INPUT_METHOD_NEEDED
        width = 0
        height = ViewGroup.LayoutParams.MATCH_PARENT
        setBackgroundDrawable(ColorDrawable(0))
        parentView.post { showAtLocation(parentView, Gravity.NO_GRAVITY, 0, 0) }
        activity.lifecycle.addObserver(object :LifecycleObserver{
            @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
            fun onDestroy(){
                PPLog.d("KeyboardHeightProvider","KeyboardHeightProvider onDestroy")
                //activity 关闭的时候释放popupWindow 防止内存泄漏
                dismiss()
            }
        })
    }
}

具体使用

activity?.let {
                KeyboardHeightProvider(
                    it,
                    binding.root,
                    object : KeyboardHeightProvider.KeyboardHeightListener {
                        override fun onKeyboardHeightChanged(
                            keyboardHeight: Int,
                            keyboardOpen: Boolean,
                            isLandscape: Boolean
                        ) {
                            if (keyboardOpen) {
                                //editText获取光标
                                editText.requestFocus()
                            } else {
                                //editText取消光标
                                editText.clearFocus()
                            }
                        }
                    }, true
                )
            }

相关文章

网友评论

      本文标题:adjustNothing时监听键盘状态及高度

      本文链接:https://www.haomeiwen.com/subject/cnvpqltx.html