首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >键盘上方的BottomSheetDialog

键盘上方的BottomSheetDialog
EN

Stack Overflow用户
提问于 2021-08-06 07:09:58
回答 1查看 67关注 0票数 0

如何才能使键盘在打开BottomSheetDialog时出现,并且对话框出现在其上方?EditText立即变得活跃起来。

现在我的对话框如下所示:

我想在这里点赞:

下面是我的代码:

代码语言:javascript
运行
复制
abstract class CustomDialog(@LayoutRes layout: Int) : DialogFragment() {
    val layoutDialog = layout
    val dialogView: View? by lazy { View.inflate(activity, layout, null) as ViewGroup }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        return dialogView
    }

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        val bottomSheetDialog = BottomSheetDialog(requireActivity(), R.style.BottomSheetDialogTheme)
        val bottomSheetView = LayoutInflater.from(context).inflate(layoutDialog, null)
        bottomSheetDialog.setContentView(bottomSheetView)

        return bottomSheetDialog
    }
}

创建对话框:

代码语言:javascript
运行
复制
class CreateTaskDialog() : CustomDialog(R.layout.dialog_add_task) {
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
    }
}

我在这里显示了这个对话框:

代码语言:javascript
运行
复制
requireActivity().showDialog(CreateTaskDialog())

fun FragmentActivity.showDialog(dialog: DialogFragment, tag: String? = null)
        = dialog.show(this.supportFragmentManager, tag)

这就是风格:

代码语言:javascript
运行
复制
<style name="BottomSheetDialogTheme" parent="Theme.Design.Light.BottomSheetDialog">
    <item name="bottomSheetStyle">@style/BottomSheetStyle</item>
</style>

<style name="BottomSheetStyle" parent="Widget.Design.BottomSheet.Modal">
    <item name="android:windowIsFloating">false</item>
    <item name="android:windowSoftInputMode">adjustResize</item>
    <item name="android:background">@android:color/transparent</item>
</style>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-08-06 17:23:59

解决方案并不明显)我已经搜索了整个堆栈溢出。我找到了几个解决方案,但没有一个对我有帮助,直到我做了如下样式(一个圆边的例子):

代码语言:javascript
运行
复制
<style name="BottomSheetDialogKeyboardTheme" parent="Theme.MaterialComponents.DayNight.BottomSheetDialog">
    <item name="android:windowIsFloating">false</item>
    <item name="android:windowSoftInputMode">adjustResize</item>
    <item name="android:statusBarColor">@android:color/transparent</item>
    <item name="android:background">@android:color/transparent</item>
    <item name="shapeAppearanceOverlay">@style/ShapeBottomSheetDialogKeyboard</item>
</style>

<style name="ShapeBottomSheetDialogKeyboard" parent="">
    <item name="cornerFamily">rounded</item>
    <item name="cornerSizeTopRight">16dp</item>
    <item name="cornerSizeTopLeft">16dp</item>
    <item name="cornerSizeBottomRight">0dp</item>
    <item name="cornerSizeBottomLeft">0dp</item>
</style>

在创建片段时,我也添加了这样一个时刻(尽管没有它也可以工作得很好):

代码语言:javascript
运行
复制
val bottomSheet = bottomSheetDialog.window?.decorView?.findViewById<FrameLayout>(R.id.design_bottom_sheet) as FrameLayout
val mBehavior = BottomSheetBehavior.from(bottomSheet)
mBehavior.state = BottomSheetBehavior.STATE_EXPANDED

为了应用圆角边缘的样式,我必须添加另一个代码,如下所示:

代码语言:javascript
运行
复制
val newMaterialShapeDrawable: MaterialShapeDrawable = createMaterialShapeDrawable(requireContext(), bottomSheet)
                ViewCompat.setBackground(bottomSheet, newMaterialShapeDrawable)

fun createMaterialShapeDrawable(context: Context, bottomSheet: View): MaterialShapeDrawable {
    val shapeAppearanceModel = ShapeAppearanceModel
        .builder(context, 0, R.style.ShapeBottomSheetDialogKeyboard)
        .build()

    val currentMaterialShapeDrawable = bottomSheet.background as MaterialShapeDrawable
    val newMaterialShapeDrawable = MaterialShapeDrawable(shapeAppearanceModel)

    newMaterialShapeDrawable.initializeElevationOverlay(context)
    newMaterialShapeDrawable.fillColor = currentMaterialShapeDrawable.fillColor
    newMaterialShapeDrawable.tintList = currentMaterialShapeDrawable.tintList
    newMaterialShapeDrawable.elevation = currentMaterialShapeDrawable.elevation
    newMaterialShapeDrawable.strokeWidth = currentMaterialShapeDrawable.strokeWidth
    newMaterialShapeDrawable.strokeColor = currentMaterialShapeDrawable.strokeColor

    return newMaterialShapeDrawable
}

整个CustomDialog代码如下所示:

代码语言:javascript
运行
复制
abstract class CustomDialogKeyboard(@LayoutRes layout: Int) : BottomSheetDialogFragment() {

    private val layoutDialog = layout
    private lateinit var mBehavior: BottomSheetBehavior<FrameLayout>

    val dialogView: View? by lazy { View.inflate(activity, layout, null) as ViewGroup }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        return dialogView
    }

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        val bottomSheetDialog = BottomSheetDialog(requireActivity(), R.style.BottomSheetDialogKeyboardTheme)
        val bottomSheetView = LayoutInflater.from(context).inflate(layoutDialog, null)
        bottomSheetDialog.setContentView(bottomSheetView)

        bottomSheetDialog.setOnShowListener {
            try {
                val bottomSheet = bottomSheetDialog.window?.decorView?.findViewById<FrameLayout>(R.id.design_bottom_sheet) as FrameLayout
                mBehavior = BottomSheetBehavior.from(bottomSheet)
                mBehavior.state = BottomSheetBehavior.STATE_EXPANDED

                val newMaterialShapeDrawable: MaterialShapeDrawable = createMaterialShapeDrawable(requireContext(), bottomSheet)
                ViewCompat.setBackground(bottomSheet, newMaterialShapeDrawable)
            } catch (e: Exception) {}
        }

        return bottomSheetDialog
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68677535

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档