Android 10 BottomSheetDialogFragment EditText和RecyclerView事件冲突,导致不能正常获取焦点(软件盘弹起后自动关闭,输入内容后失去焦点等问题)

博客主要讲述了Android 10中BottomSheetDialogFragment里EditText和RecyclerView事件冲突问题,如键盘不能顶起布局、焦点丢失等,还给出了相应解决办法,包括设置样式、监听焦点重新设置、在RecyclerView布局加入特定内容及设置EditText点击事件等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Android 10 BottomSheetDialogFragment EditText和RecyclerView事件冲突,导致不能正常获取焦点(软件盘弹起后自动关闭,输入内容后失去焦点等问题)

问题一:

键盘不能顶起布局

解决方法:

一般的设置不能解决,需要设置BottomSheetDialogFragment 样式

    <style name="Theme.Design.Light.BottomSheetDialog.WithoutBehavior">
        <item name="bottomSheetStyle">@null</item>
        <item name="android:elevation" ns1:ignore="NewApi">16dp</item>
        <item name="android:windowIsFloating">false</item>
        <item name="android:windowSoftInputMode">stateHidden|adjustResize</item>
    </style>

问题二:

焦点丢失

解决方法:

一:监听焦点丢失时,将其重新设置

   var editValueView: AppCompatEditText? = null
    var isDismission = false  
editValue.apply {                       
                            setOnClickListener{
                                this.openInputMethod()
                            }
                            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
                                onFocusChangeListener = OnFocusChangeListener { v, hasFocus ->
                                    if (!hasFocus) {
                                        if (!isDismission)
                                            this.openInputMethod()
                                    } else {
                                        (v as EditText).setSelection(v.text.length)
                                    }
                                }
                            }
                        }
 override fun onDismiss(dialog: DialogInterface) {
            isDismission = true
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
                editValueView?.closeInputMethodAndClearFocus()
            }
        super.onDismiss(dialog)
    }

openInputMethod:

fun View.openInputMethod(force: Boolean = false) {
    if (force || !isFocused) {
        isFocusable = true
        isFocusableInTouchMode = true
        requestFocus()
        (this as? EditText)?.isCursorVisible = true
        postDelayed({
            (context.getSystemService(INPUT_METHOD_SERVICE) as InputMethodManager).showSoftInput(this@openInputMethod, InputMethodManager.SHOW_FORCED)
        }, 100)
    }
}
closeInputMethodAndClearFocus:
fun EditText.closeInputMethodAndClearFocus() {
    closeInputMethod().apply {
        isCursorVisible = false
        isFocusable = false
        isFocusableInTouchMode = false
        clearFocus()
    }
}

这个方法的缺点是不完美,没有根本解决

方法二:

RecyclerView布局加入:

        android:descendantFocusability="beforeDescendants"
        android:fastScrollEnabled="false"

同时设置EditText点击事件

 editValue.apply {
    setOnClickListener{
                                openInputMethod()
                            }
}

 

fun View.openInputMethod(force: Boolean = false) {
    if (force || !isFocused) {
        isFocusable = true
        isFocusableInTouchMode = true
        requestFocus()
        (this as? EditText)?.isCursorVisible = true
        postDelayed({
            (context.getSystemService(INPUT_METHOD_SERVICE) as InputMethodManager).showSoftInput(this@openInputMethod, InputMethodManager.SHOW_FORCED)
        }, 100)
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值