【问题标题】:How to show EditText error below the layout instead of a popup? [duplicate]如何在布局下方显示 EditText 错误而不是弹出窗口? [复制]
【发布时间】:2020-05-19 01:12:33
【问题描述】:

当用户输入错误值时,我使用了 Material TextInputLayout 来显示错误。虽然错误逻辑工作正常,但错误显示在类似this 的弹出窗口中,而不是在布局下显示为this。谁能解释我做错了什么。这就是我在布局 xml 文件中创建 EditText 的方式:

<com.google.android.material.textfield.TextInputLayout
        android:id="@+id/password_layout_signup_page"
        style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="32dp"
        android:layout_marginEnd="32dp"
        app:errorEnabled="true"
        app:passwordToggleEnabled="true"
        android:layout_below="@id/email_layout_signup_page">
 <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/password_edit_signup_page"
            style="@style/Body2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:drawableStart="@drawable/lock_icon"
            android:drawablePadding="8dp"
            android:drawableTint="@color/hint_gray"
            android:ems="10"
            android:hint="@string/password_asteriks"
            android:inputType="textPassword" />
    </com.google.android.material.textfield.TextInputLayout>

这就是我在相应的 .kt 文件中设置错误的方式:

passwordEditText.setError("Be at least 8 characters",null)

【问题讨论】:

    标签: android kotlin android-edittext material-design


    【解决方案1】:

    TextInputLayout 而不是TextInputEditText 上设置错误

    所以试试这个:

    password_layout_signup_page.setError("Be at least 8 characters")
    

    【讨论】:

    • 非常感谢@nimi0112。 stackoverflow 不允许我在 5 分钟内接受您的回答。限时结束,我会接受的。
    • @Yahya 如果您想更改整个应用程序中的错误颜色,请使用此stackoverflow.com/a/42018351/6549598。如果您只想在 1 个地方更改颜色,请使用此 stackoverflow.com/a/30953551/6549598
    • 大哥万分感谢......上帝保佑你
    猜你喜欢
    • 1970-01-01
    • 2021-10-22
    • 1970-01-01
    • 1970-01-01
    • 2014-05-08
    • 1970-01-01
    • 2018-08-14
    • 2020-08-26
    • 1970-01-01
    相关资源
    最近更新 更多