如何才能使键盘在打开BottomSheetDialog时出现,并且对话框出现在其上方?EditText立即变得活跃起来。
现在我的对话框如下所示:
我想在这里点赞:
下面是我的代码:
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
}
}
创建对话框:
class CreateTaskDialog() : CustomDialog(R.layout.dialog_add_task) {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
}
}
我在这里显示了这个对话框:
requireActivity().showDialog(CreateTaskDialog())
fun FragmentActivity.showDialog(dialog: DialogFragment, tag: String? = null)
= dialog.show(this.supportFragmentManager, tag)
这就是风格:
<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>
发布于 2021-08-06 17:23:59
解决方案并不明显)我已经搜索了整个堆栈溢出。我找到了几个解决方案,但没有一个对我有帮助,直到我做了如下样式(一个圆边的例子):
<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>
在创建片段时,我也添加了这样一个时刻(尽管没有它也可以工作得很好):
val bottomSheet = bottomSheetDialog.window?.decorView?.findViewById<FrameLayout>(R.id.design_bottom_sheet) as FrameLayout
val mBehavior = BottomSheetBehavior.from(bottomSheet)
mBehavior.state = BottomSheetBehavior.STATE_EXPANDED
为了应用圆角边缘的样式,我必须添加另一个代码,如下所示:
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代码如下所示:
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
}
}
https://stackoverflow.com/questions/68677535
复制相似问题