美文网首页
Android 10 BottomSheetDialogFrag

Android 10 BottomSheetDialogFrag

作者: SmallWalnutBG | 来源:发表于2020-12-02 10:54 被阅读0次

    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"
            android:focusableInTouchMode="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)
        }
    }
    

    相关文章

      网友评论

          本文标题:Android 10 BottomSheetDialogFrag

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