Android - 在键盘上方显示 BottomSheetDialogFragment

标签 android android-softkeyboard bottom-sheet

我正在尝试显示一个 BottomSheetDialogFragment,其中包含一些 EditText 字段供用户输入信息。我想直接在键盘上方显示它,但它一直覆盖内容。

这是我调出 BottomSheetDialogFragment 时发生的情况,您可以看到它正在选择 Card Number EditText,但覆盖了其他内容。

BottomSheetDialogFragment covering up content

理想情况下,这就是我要找的东西,您可以同时看到 EditTexts 和 View 的填充。

BottomSheetDialogFragment not covering up content

我已经尝试了很多围绕 windowSoftInputMode 的解决方案,但似乎没有任何效果。我已经为父级 Activity 和实际的 BottomSheetDialogFragment 通过

将其设置为 adjustResize

dialog.window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE)

而且我还尝试修改我的布局,将其从 FrameLayout 更改为 ScrollView 再到 CoordinatorLayout 以查看是否对布局的位置有任何影响,但似乎没有任何效果。

如果有人知道如何完成这个,那将不胜感激,谢谢。

最佳答案

也许是一个迟到的答案,但可能会有所帮助。

没有别的对我有用。但是这个解决方案是有效的

内部样式:

<style>  // main theme
    <item name="bottomSheetDialogTheme">@style/BottomSheetDialogTheme</item>
    ........ // rest of the code
</style>

<style name="BottomSheetDialogTheme" parent="Theme.Design.Light.BottomSheetDialog">
    <item name="bottomSheetStyle">@style/AppModalStyle</item>
    <item name="android:windowIsFloating">false</item>
    <item name="android:windowSoftInputMode">adjustResize</item>
    <item name="android:statusBarColor">@android:color/transparent</item> 
</style>

这里 android:windowIsFloating 应该是 false & android:windowSoftInputMode 必须是 adjustResize

<style name="AppModalStyle" parent="Widget.Design.BottomSheet.Modal">
    <item name="android:background">@drawable/rounded_corner_dialog</item>
</style>

注意:这部分是可选的,但如果事情不完美,那么在 NestedScrollView 中包装布局

<androidx.core.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    
     <--Rest of the layout-->
</androidx.core.widget.NestedScrollView>

在某些设备上,此解决方案还不够。将此添加到代码中完全解决了我的问题。

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
    dialog?.setOnShowListener {
        val dialog = it as BottomSheetDialog
        val bottomSheet = dialog.findViewById<View>(R.id.design_bottom_sheet)
        bottomSheet?.let { sheet ->
            dialog.behavior.state = BottomSheetBehavior.STATE_EXPANDED
            sheet.parent.parent.requestLayout()
        }
    }
}

关于Android - 在键盘上方显示 BottomSheetDialogFragment,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50223392/

相关文章:

android - 为每个 android 风格使用不同的库模块

java - addParentStack 中的 NameNotFoundException

android - BottomSheet查看Android

android - 如何从RecyclerView读取数据并在BottomSheet中发送

android - BottomSheetDialogFragment 未显示

android - 为什么 Cast MediaRouteButton 即使在 Holo.Light Action-Bar 上也总是白色的?

Android 数据绑定(bind) - 'No resource identifier found for attribute'

Android:如何关闭明确显示的软键盘?

android - Android XWalkView 或 WebView 中隐藏在键盘下方的文本框

android - 隐藏在软键盘后面的日期选择器对话框