adjustNothing時(shí)監(jiān)聽鍵盤狀態(tài)及高度

背景

開發(fā)直播需求時(shí),要求直播間內(nèi)鍵盤彈起不壓縮界面,也就是不能用windowSoftInputMode#adjustResize這個(gè)屬性,因?yàn)樗麜?huì)壓縮界面。要不壓縮界面肯定就要用adjustNothing,也就是在這個(gè)模式下獲取鍵盤彈起高度,下面就是具體實(shí)現(xiàn)。

效果圖

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

代碼實(shí)現(xiàn)

/**
 * 獲取鍵盤高度幫助類
 *
 * @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 關(guān)閉的時(shí)候釋放popupWindow 防止內(nèi)存泄漏
                dismiss()
            }
        })
    }
}

具體使用

activity?.let {
                KeyboardHeightProvider(
                    it,
                    binding.root,
                    object : KeyboardHeightProvider.KeyboardHeightListener {
                        override fun onKeyboardHeightChanged(
                            keyboardHeight: Int,
                            keyboardOpen: Boolean,
                            isLandscape: Boolean
                        ) {
                            if (keyboardOpen) {
                                //editText獲取光標(biāo)
                                editText.requestFocus()
                            } else {
                                //editText取消光標(biāo)
                                editText.clearFocus()
                            }
                        }
                    }, true
                )
            }
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容