更改textInputLayout的笔触颜色

时间:2020-08-27 14:36:21

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

发生错误时(例如,当用户输入无效的名称(插入符号,例如:@ $等)时),我想使textBox的笔触颜色为红色,我该怎么做,这是我的文本框代码

<com.google.android.material.textfield.TextInputLayout
    android:id="@+id/etFullNameLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="Full Name"
    android:textColorHint="#b4ffff"
    app:startIconTint="#b4ffff"
    android:layout_margin="20dp"
    app:startIconDrawable="@drawable/ic_person"
    app:boxStrokeErrorColor="@color/color_on_secondary" 

    style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox">

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

        />

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

1 个答案:

答案 0 :(得分:0)

带有错误的默认笔触颜色是红色。
您可以在任何情况下使用 boxStrokeErrorColor 属性对其进行自定义:

<com.google.android.material.textfield.TextInputLayout
    app:boxStrokeErrorColor="@color/..." 
    ...>

然后,您必须提供自定义验证。像这样:

    etFullName.addTextChangedListener(
        afterTextChanged = {
            if (!isValid(it)){
                etFullNameLayout.error = "Error!"
            }
        }
    )

enter image description here