【问题标题】:Changing Stroke color of a textInputLayout更改 textInputLayout 的笔触颜色
【发布时间】:2020-12-16 11:28:20
【问题描述】:

当发生错误时(例如当用户输入无效名称时(插入符号如:@$ 等),我想让 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>

【问题讨论】:

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


    【解决方案1】:

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

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

    然后您必须提供您的自定义验证。比如:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-07-18
      • 2016-10-22
      • 2017-04-08
      • 2019-04-27
      • 2018-11-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多