Android - 在键盘上方显示BottomSheetDialogFragment

Adv*_*Dog 12 android android-softkeyboard bottom-sheet

我正在尝试显示一个BottomSheetDialogFragment带有几个EditText字段供用户输入信息.我想直接在键盘上方显示它,但它会不断覆盖内容.

这就是当我提出时BottomSheetDialogFragment,你可以看到它正在选择Card Number EditText,但覆盖其他内容.

BottomSheetDialogFragment覆盖内容

理想情况下,这就是我正在寻找的,你可以看到它们EditTexts和视图的填充.

BottomSheetDialogFragment没有掩盖内容

我已经尝试了很多解决方案windowSoftInputMode,但似乎没有任何效果.我已将其设置adjustResize为父级Activity和实际的BottomSheetDialogFragment通道

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

我也试着改变我的布局,从改变它FrameLayout,到ScrollView一个CoordinatorLayout,看看是否能对布局的地位产生任何影响,但似乎没有任何工作.

如果有人知道如何做到这一点,那将非常感谢,谢谢.

Muh*_*que 33

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

没有其他东西对我有用。但是这个解决方案是有效的

内搭款式:

<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>
Run Code Online (Sandbox Code Playgroud)

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

<style name="AppModalStyle" parent="Widget.Design.BottomSheet.Modal">
    <item name="android:background">@drawable/rounded_corner_dialog</item>
</style>
Run Code Online (Sandbox Code Playgroud)

在 NestedScrollView 中包装布局

<androidx.core.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

     <--Rest of the layout-->
</androidx.core.widget.NestedScrollView>
Run Code Online (Sandbox Code Playgroud)

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

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()
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 对我来说,NestedScrollView 很麻烦,因为我里面有回收器视图。保持布局与以前相同,仅更改样式以及以编程方式处理 BottomSheetDialogFragment 非常有效!谢谢 (3认同)
  • 我对此表示怀疑,但这确实产生了奇迹。我发现我不需要更改布局来使用“NestedScrollView”,或添加“onViewCreated”片段 - 仅样式就足够了 (2认同)

小智 3

以下步骤可能有助于解决您的问题。

首先在 xml 文件中的 EditText 上添加以下行

android:imeOptions="actionSend|flagNoEnterAction"
Run Code Online (Sandbox Code Playgroud)

例如:

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inputType="number"
    android:hint="Card number"
    android:imeOptions="actionSend|flagNoEnterAction"/>
Run Code Online (Sandbox Code Playgroud)

然后在 AndroidManifest.xml 文件中的活动标记上添加以下行

android:windowSoftInputMode="stateVisible|adjustResize"
Run Code Online (Sandbox Code Playgroud)

例如:

<activity android:name=".stack.MainActivity"
    android:windowSoftInputMode="stateVisible|adjustResize">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>
Run Code Online (Sandbox Code Playgroud)

您可以从此处找到有关屏幕输入法的更多详细信息。

  • 将“actionSend”添加到“imeOptions”与此问题无关。 (7认同)