BottomSheetDialogFragment中EditText問題

最近在做一個評論彈框的時候遇到兩個問題:

1.BottomSheetDialogFragment中的EditText有行數顯示,當內容過多後,滑動內容和BottomSheetDialogFragment的上下拖動事件衝突了。解決辦法:禁止BottomSheetDialogFragment的上下拖動功能

設置回掉監聽:

override var mBottomSheetBehaviorCallback: BottomSheetBehavior.BottomSheetCallback? = object : BottomSheetBehavior.BottomSheetCallback() {
        override fun onStateChanged(bottomSheet: View, newState: Int) {
            if (newState == BottomSheetBehavior.STATE_DRAGGING || newState == BottomSheetBehavior.STATE_SETTLING) {
                mBehavior?.state = BottomSheetBehavior.STATE_COLLAPSED
            }
        }

        override fun onSlide(bottomSheet: View, slideOffset: Float) {}
    }
        mBehavior = BottomSheetBehavior.from(rootView?.parent as View)
        // 直接展開內容
        mBehavior?.state = BottomSheetBehavior.STATE_EXPANDED
        mBehavior?.isHideable = true
        (rootView?.parent as View).setBackgroundColor(Color.TRANSPARENT)
        // 設置內容高度
        rootView?.post {
            mBehavior?.setPeekHeight(rootView?.height ?: 0)
        }
        // 設置監聽
        mBottomSheetBehaviorCallback?.let {
            mBehavior?.addBottomSheetCallback(it)
        }

然後就解決了。

2.輸入框被擋住問題:

<style name="BottomSheetDialogStyle" parent="Theme.Design.Light.BottomSheetDialog">
        <item name="android:windowIsFloating">false</item>
        <item name="android:statusBarColor">@android:color/transparent</item>
        <item name="android:windowSoftInputMode">adjustResize</item>
    </style>
override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setStyle(DialogFragment.STYLE_NORMAL, R.style.BottomSheetDialogStyle)
    }

參考自:https://stackoverflow.com/questions/48002290/show-entire-bottom-sheet-with-edittext-above-keyboard

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章