如何防止键盘打开时MaterialAlertDialogBuilder高度自动调整大小?

问题描述 投票:0回答:1

我使用 MaterialAlertDialogBuilder 构建了一个自定义对话框。该对话框有 2 个 TextInputLayout。问题是当我尝试在 TextInputEditText 中写入内容并且键盘可见时,MaterialAlertDialogBuilder 的高度会缩小。

在 XML 代码中:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="16dp"
    tools:viewBindingIgnore="true">

    <com.google.android.material.textfield.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="দোয়া"
        app:endIconMode="clear_text"
        app:shapeAppearance="@style/ShapeAppearance.App.SmallComponent"
        tools:viewBindingIgnore="true">

        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/doarName"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

    </com.google.android.material.textfield.TextInputLayout>

    <com.google.android.material.textfield.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="11dp"
        android:hint="দৈনিক কতবার পাঠ করবেন?"
        app:endIconMode="clear_text"
        app:shapeAppearance="@style/ShapeAppearance.App.SmallComponent"
        tools:viewBindingIgnore="true">

        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/number"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="number" />

    </com.google.android.material.textfield.TextInputLayout>

</LinearLayout>


在 Kotlin 中:

MaterialAlertDialogBuilder(this@TosbiActivity).setView(view).setTitle("দোয়া যুক্ত করুন")
                .setPositiveButton("Apply") { dialog, _ ->

                    /*
                    ......
                     */

                    dialog.dismiss()
                }.setNegativeButton("Cancel") { dialog, _ ->
                    dialog.dismiss()
                }.show()

代码的输出:

(当键盘隐藏时)

(当键盘可见时)

我尝试了什么?

我通过添加

android:windowSoftInputMode="adjustResize"/>
android:windowSoftInputMode="adjustPan"
代码进行了检查 清单的活动标记,但它不起作用。 我也在互联网+ChatGPT AI上搜索或询问了这个问题,但没有得到正确的答案。

android kotlin android-textinputlayout material-dialog
1个回答
0
投票

经过大量研究,我找出了问题的原因。

问题是由于设备的屏幕尺寸造成的。我在比我的设备屏幕尺寸更大的设备上检查过,没有出现该问题。

要避免此问题:可以使用

AlertDialog.Builder
代替
MaterialAlertDialogBuilder
,或者您可以通过将此代码添加到您的
MaterialAlertDialogBuilder
来解决问题。

.window?.setFlags(
                    WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,
                    WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS
                )

完整的代码如下所示:

val view = layoutInflater.inflate(R.layout.add_doa, null)

            MaterialAlertDialogBuilder(this@TosbiActivity).setView(view).setTitle("দোয়া যুক্ত করুন")
                .setPositiveButton("Apply") { dialog, _ ->

                    /*
                    ......
                     */

                    dialog.dismiss()
                }.setNegativeButton("Cancel") { dialog, _ ->
                    dialog.dismiss()
                }.show().window?.setFlags(
                    WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,
                    WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS
                )
© www.soinside.com 2019 - 2024. All rights reserved.