当我启用它时,TextInputLayout 视图边框周围的边框消失了

Border around TextInputLayout view border disappears when I enable it

当我将输入布局的错误设置为启用时,我想要发生的是填充颜色变成浅红色,边框变成深色。

但我最终得到的是浅色阴影,但没有边框。

这是我的布局

        <android.support.design.widget.TextInputLayout
        android:id="@+id/five_star_review_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:errorEnabled="true"
        app:hintEnabled="false"
        app:errorTextAppearance="@style/ErrorTextFiveStar"
        android:importantForAccessibility="no"
        android:labelFor="@+id/add_number"
        tools:error="true">

        <android.support.design.widget.TextInputEditText
            android:id="@+id/five_star_review_text"
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:background="@drawable/border"
            android:padding="13dp"
            style="@style/StarRatingInputTextStyle"
            tools:text="I really like the video layout. It was easy to understand, and very intuitive. The invitation via email" />

这是我的代码。

 five_star_review_text.addTextChangedListener(object : TextWatcher{
        override fun afterTextChanged(s: Editable?) {
        }

        override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
        }

        override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
            if (s.length > 100) {
                five_star_review_layout.error = " "
                five_star_review_layout.boxStrokeColor= ContextCompat.getColor(activity!!, R.color.error_box_selector)
            }
            else {
                five_star_review_layout
                    .error =""
                five_star_review_layout.boxStrokeColor = ContextCompat.getColor(activity!!, R.color.error_box_selector)
            }
            if ((100 - s.length) > 0) {
                val counter = "+${100 - s.length}"
                review_counter.text = counter
                review_counter.setTextColor(ContextCompat.getColor(activity!!, R.color.black))
            } else {
                review_counter.text ="${100 - s.length}"
                review_counter.setTextColor(ContextCompat.getColor(activity!!, R.color.kp_red))
            }
        }

    })

这是我的选择器

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_enabled="true" android:color="@color/kp_red"/>
    <item android:state_hovered="true" android:color="@color/kp_red"/>
    <item android:state_focused="true" android:color="@color/kp_red"/>
    <item android:color="@color/kp_red"/>
</selector>

要定义错误时的笔触颜色,您可以使用 boxStrokeErrorColor 属性。 您还可以使用 app:counterEnabledapp:counterMaxLength 属性来自动检测太长的文本。

<com.google.android.material.textfield.TextInputLayout
        app:boxStrokeErrorColor="@color/...."
        app:counterEnabled="true"
        app:counterMaxLength="100"
        style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox">

        <com.google.android.material.textfield.TextInputEditText
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>

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

如果您还想更改背景颜色,可以使用方法

  • textInputLayout.setBoxBackgroundColor
  • textInputLayout.setBoxBackgroundColorStateList
  • textInputLayout.setBoxBackgroundColorResource

注意: 这需要版本 1.2.0(当前 1.2.0-beta01)。