【问题标题】:Android Material TextInputLayout error message is displayed behinde Material TextInputEditText childAndroid Material TextInputLayout 错误信息显示在 Material TextInputEditText child 后面
【发布时间】:2023-04-10 15:19:01
【问题描述】:

我目前正在使用 Material 组件创建一个 Android UI。 在我的活动中,我使用了 com.google.android.material.textfield.TextInputLayout-component 的错误功能。

问题在于,如果许多组件显示错误,则错误消息将无法正确显示。看起来消息在 material.textfield.TextInputEditText 后面,它是 TextInputLayout 的子项。请查看屏幕截图以进行说明。

Screenshot

有什么猜测吗?

   <?xml version="1.0" encoding="utf-8"?>
<ScrollView 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="match_parent"
    tools:context=".ui.createOffer.CreateOfferActivity">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="6dp"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <ImageView
            android:id="@+id/offerImage"
            android:layout_width="match_parent"
            android:layout_height="230dp"
            android:contentDescription="@string/postOffer_Image"
            android:scaleType="centerInside"
            app:layout_constraintBottom_toTopOf="@id/layout_Description"
            app:layout_constraintTop_toBottomOf="@id/progressBar"
            tools:srcCompat="@tools:sample/avatars" />

        <ProgressBar
            android:id="@+id/progressBar"
            style="@style/Widget.AppCompat.ProgressBar.Horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentStart="true"
            android:elevation="10dp"
            android:indeterminate="true"
            android:max="100"
            android:visibility="gone"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <com.google.android.material.button.MaterialButton
            android:id="@+id/createOffer"
            style="@style/Widget.MaterialComponents.Button.OutlinedButton"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="@dimen/component_label_horizontal_margin"
            android:text="@string/postOffer_createOfferButton_Text"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintTop_toBottomOf="@id/layout_PurchaseDate" />

        <com.google.android.material.textfield.TextInputLayout
            android:id="@+id/layout_Price"
            style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="@string/postOffer_editTextPrice_hint"
            app:errorEnabled="true"
            app:layout_constraintBottom_toTopOf="@id/layout_Category"
            app:layout_constraintTop_toBottomOf="@id/layout_Description">

            <com.google.android.material.textfield.TextInputEditText
                android:id="@+id/editText_price"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:importantForAutofill="no"
                android:inputType="numberDecimal" />
        </com.google.android.material.textfield.TextInputLayout>

        <com.google.android.material.textfield.TextInputLayout
            android:id="@+id/layout_Category"
            style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.ExposedDropdownMenu"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:focusable="false"
            android:focusableInTouchMode="false"
            android:hint="@string/postOffer_Category"
            android:inputType=""
            app:errorEnabled="true"
            app:layout_constraintBottom_toTopOf="@id/layout_Price"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/layout_ExpireDate">

            <AutoCompleteTextView
                android:id="@+id/filled_exposed_dropdown_ctagory"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:focusableInTouchMode="false"
                android:importantForAutofill="no" />

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

        <com.google.android.material.textfield.TextInputLayout
            android:id="@+id/layout_ExpireDate"
            style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="8dp"
            android:hint="@string/postOffer_ExpireDateHint"
            android:inputType="none"
            app:errorEnabled="true"
            app:layout_constraintBottom_toTopOf="@id/layout_PurchaseDate"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/layout_Category">

            <com.google.android.material.textfield.TextInputEditText
                android:id="@+id/editText_expireDate"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:focusable="false"
                android:focusableInTouchMode="false"
                android:importantForAutofill="no"
                android:inputType="date" />
        </com.google.android.material.textfield.TextInputLayout>

        <com.google.android.material.textfield.TextInputLayout
            android:id="@+id/layout_PurchaseDate"
            style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:cursorVisible="false"
            android:hint="@string/postOffer_PurchaseDateHint"
            android:inputType="none"
            app:errorEnabled="true"
            app:layout_constraintBottom_toTopOf="@id/createOffer"
            app:layout_constraintTop_toBottomOf="@id/layout_ExpireDate">

            <com.google.android.material.textfield.TextInputEditText
                android:id="@+id/editText_purchaseDate"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:focusableInTouchMode="false"
                android:importantForAutofill="no"
                android:inputType="date" />
        </com.google.android.material.textfield.TextInputLayout>

        <com.google.android.material.textfield.TextInputLayout
            android:id="@+id/layout_Description"
            style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="@string/general_description"
            android:inputType="textMultiLine"
            android:maxLines="5"
            app:counterEnabled="true"
            app:counterMaxLength="255"
            app:errorEnabled="true"
            app:layout_constraintBottom_toTopOf="@id/layout_Price"
            app:layout_constraintTop_toBottomOf="@id/offerImage">

            <com.google.android.material.textfield.TextInputEditText
                android:id="@+id/description_EditText"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:importantForAutofill="no" />
        </com.google.android.material.textfield.TextInputLayout>
    </androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>

问候:)

【问题讨论】:

  • 为什么不添加一些边距/填充?
  • 我已经尝试过使用边距和填充。但这不会解决问题。还有其他想法吗?
  • 尝试垂直线性布局。或者,如果整个屏幕已满,布局可能会缩小。尝试将其包装在 ScrollView 中。如果两者都没有帮助,请发布整个 xml。
  • 垂直线性布局没有解决问题。我将整个 xml 放入原始问题中,因为它对于答案字段来说太长了。
  • 我的新答案有效吗?

标签: android android-layout material-design android-constraintlayout android-textinputlayout


【解决方案1】:

我认为您不小心连接了约束布局的错误点。 比较以下几行:

android:id="@+id/layout_Category"
app:layout_constraintBottom_toTopOf="@id/layout_ExpireDate"
app:layout_constraintTop_toBottomOf="@id/layout_Price">
<!--            app:layout_constraintBottom_toTopOf="@id/layout_Price"-->
<!--            app:layout_constraintTop_toBottomOf="@id/layout_ExpireDate"-->

它可以在我的电脑上运行。但是我没有你的字符串和图像。 我也增加了一些边距。

[Screenshot]1

完整代码:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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="match_parent">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="6dp">

        <ImageView
            android:id="@+id/offerImage"
            android:layout_width="match_parent"
            android:layout_height="230dp"
            android:scaleType="centerInside"
            app:layout_constraintBottom_toTopOf="@id/layout_Description"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/progressBar"
            tools:srcCompat="@tools:sample/avatars" />

        <ProgressBar
            android:id="@+id/progressBar"
            style="@style/Widget.AppCompat.ProgressBar.Horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentStart="true"
            android:elevation="10dp"
            android:indeterminate="true"
            android:max="100"
            android:visibility="gone"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <com.google.android.material.button.MaterialButton
            android:id="@+id/createOffer"
            style="@style/Widget.MaterialComponents.Button.OutlinedButton"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            android:layout_marginBottom="24dp"
            android:text="string/postOffer_createOfferButton_Text"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintTop_toBottomOf="@id/layout_PurchaseDate" />
        <!--            android:layout_marginBottom="@dimen/component_label_horizontal_margin"-->

        <com.google.android.material.textfield.TextInputLayout
            android:id="@+id/layout_Description"
            style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            android:layout_marginBottom="8dp"
            android:hint="string/general_description"
            android:inputType="textMultiLine"
            android:maxLines="5"
            app:counterEnabled="true"
            app:counterMaxLength="255"
            app:errorEnabled="true"
            app:layout_constraintBottom_toTopOf="@id/layout_Price"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/offerImage">

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

        <com.google.android.material.textfield.TextInputLayout
            android:id="@+id/layout_Price"
            style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            android:layout_marginBottom="8dp"
            android:hint="string/postOffer_editTextPrice_hint"
            app:errorEnabled="true"
            app:layout_constraintBottom_toTopOf="@id/layout_Category"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/layout_Description">

            <com.google.android.material.textfield.TextInputEditText
                android:id="@+id/editText_price"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:importantForAutofill="no"
                android:inputType="numberDecimal" />
        </com.google.android.material.textfield.TextInputLayout>

        <com.google.android.material.textfield.TextInputLayout
            android:id="@+id/layout_Category"
            style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.ExposedDropdownMenu"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            android:layout_marginBottom="8dp"
            android:focusable="false"
            android:focusableInTouchMode="false"
            android:hint="string/postOffer_Category"
            android:inputType=""
            app:errorEnabled="true"
            app:layout_constraintBottom_toTopOf="@id/layout_ExpireDate"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/layout_Price">
            <!--            app:layout_constraintBottom_toTopOf="@id/layout_Price"-->
            <!--            app:layout_constraintEnd_toEndOf="parent"-->
            <!--            app:layout_constraintStart_toStartOf="parent"-->
            <!--            app:layout_constraintTop_toBottomOf="@id/layout_ExpireDate"-->
            <AutoCompleteTextView
                android:id="@+id/filled_exposed_dropdown_ctagory"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:focusableInTouchMode="false"
                android:importantForAutofill="no" />

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

        <com.google.android.material.textfield.TextInputLayout
            android:id="@+id/layout_ExpireDate"
            style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            android:layout_marginBottom="8dp"
            android:hint="string/postOffer_ExpireDateHint"
            android:inputType="none"
            app:errorEnabled="true"
            app:layout_constraintBottom_toTopOf="@id/layout_PurchaseDate"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/layout_Category">

            <com.google.android.material.textfield.TextInputEditText
                android:id="@+id/editText_expireDate"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:focusable="false"
                android:focusableInTouchMode="false"
                android:importantForAutofill="no"
                android:inputType="date" />
        </com.google.android.material.textfield.TextInputLayout>

        <com.google.android.material.textfield.TextInputLayout
            android:id="@+id/layout_PurchaseDate"
            style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            android:layout_marginBottom="8dp"
            android:cursorVisible="false"
            android:hint="string/postOffer_PurchaseDateHint"
            android:inputType="none"
            app:errorEnabled="true"
            app:layout_constraintBottom_toTopOf="@id/createOffer"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/layout_ExpireDate">

            <com.google.android.material.textfield.TextInputEditText
                android:id="@+id/editText_purchaseDate"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:focusableInTouchMode="false"
                android:importantForAutofill="no"
                android:inputType="date" />
        </com.google.android.material.textfield.TextInputLayout>

    </androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>

【讨论】:

  • 我用你的代码试过了。但仍然发生同样的错误。我实际上缩短了文本的长度,但没有任何变化。如果我提供字符串和我的主题会有所帮助吗?
【解决方案2】:

将错误设置为您的 TextInputEditText 而不是 TextInputLayout,

 description_EditText.setError("Error Message");

【讨论】:

  • 这只是一种解决方法,因为这会改变错误消息的行为。我稍后会提供截图,但不幸的是这不是解决方案:/
  • 这是唯一的出路,根据您应用的样式。您可以选择 Toast 或 Snackbar 来显示错误消息。
  • 好的,感谢您的帮助。正如您在屏幕截图中看到的那样,遗憾的是,在我的情况下,无法使用 Toast 或 Snackbar。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-07-01
  • 2016-11-13
  • 2017-04-18
  • 2018-03-15
  • 2020-09-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多