如何更改笔触颜色?自定义带有材质样式的 TextInputLayout

时间:2021-03-18 07:36:31

标签: android xml android-layout kotlin material-design

我想要的设计在图片中 enter image description here

我试图继承 boxstroke 颜色但它也没有改变它仍然显示红色轮廓 去除背景颜色的一种方法是调整TextInputLayout的背景模式。默认背景颜色基于colorOnSurface属性。我可以使用 boxBackgroundColor 属性(在布局或自定义样式中,但它也不起作用

验证码

fun EditText.validateWithTextWatcher(
    mTextInputLayout: TextInputLayout,
    message: String,
    validator: (String) -> Boolean
): Boolean {
    this.afterTextChanged {
        mTextInputLayout.error = if (validator(it)) {
            this.setBackgroundResource(R.drawable.bg_solid_textfield)
            this.setTextColor(ContextCompat.getColor(this.context, R.color.colorWhite))
            null
        } else {
            this.setBackgroundResource(R.drawable.bg_error_validation)
            this.setTextColor(ContextCompat.getColor(this.context, R.color.colorWhite))
            message
        }
    }
    mTextInputLayout.error = if (validator(this.getStringTrim())) {
        this.setBackgroundResource(R.drawable.bg_solid_textfield)
        this.setTextColor(ContextCompat.getColor(this.context, R.color.colorWhite))
        null
    } else {
        this.setBackgroundResource(R.drawable.bg_error_validation)
        this.setTextColor(ContextCompat.getColor(this.context, R.color.colorWhite))
        message
    }

    return validator(this.getStringTrim())
}
fun AutoCompleteTextView.clearError() {
    setBackgroundResource(R.drawable.bg_solid_textfield)
    setTextColor(ContextCompat.getColor(this.context, R.color.colorWhite))
}
fun AutoCompleteTextView.validateWithTextWatcher(
    mTextInputLayout: TextInputLayout,
    message: String,
    validator: (String) -> Boolean
): Boolean {
    this.afterTextChanged {
        mTextInputLayout.error = if (validator(it)) {
            this.setBackgroundResource(R.drawable.bg_solid_textfield)
            this.setTextColor(ContextCompat.getColor(this.context, R.color.colorWhite))
            null
        } else {
            this.setBackgroundResource(R.drawable.bg_error_validation)
            this.setTextColor(ContextCompat.getColor(this.context, R.color.colorWhite))
            message
        }
    }
    mTextInputLayout.error = if (validator(this.getStringTrim())) {
        this.setBackgroundResource(R.drawable.bg_solid_textfield)
        this.setTextColor(ContextCompat.getColor(this.context, R.color.colorWhite))
        null
    } else {
        this.setBackgroundResource(R.drawable.bg_error_validation)
        this.setTextColor(ContextCompat.getColor(this.context, R.color.colorWhite))
        message
    }

    return validator(this.getStringTrim())
}

XML 代码

 <com.google.android.material.textfield.TextInputLayout
                    android:id="@+id/til_pass_login"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="@dimen/default_margin.24"
                    android:layout_marginTop="@dimen/default_margin.24"
                    android:layout_marginEnd="@dimen/default_margin.24"
                    app:errorEnabled="true"
                    app:hintEnabled="false"
                    android:background="@color/colorTransparent"
                    android:backgroundTint="@color/colorTransparent"
                    app:passwordToggleEnabled="true"
                    app:passwordToggleTint="@color/colorPasswordToggle"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toBottomOf="@+id/til_email_login">

bg_error_validation

 <?xml version="1.0" encoding="utf-8"?><!--  res/drawable/rounded_edittext.xml -->
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:padding="10dp"
        android:shape="rectangle">
        <corners android:radius="4dp" />
        <solid android:color="@color/colorEdittextBackground" />
        <stroke
            android:width="1dip"
            android:color="@color/colorEdittextBackground" />
    </shape>

bg_solid_textfield

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle">
            <solid android:color="@color/colorEdittextBackground"/>
            <corners android:radius="4dp" />
            <padding
                android:bottom="1.2dp"
                android:left="1.2dp"
                android:right="1.2dp"
                android:top="1.2dp" />
            <size
                android:width="162dp"
                android:height="44dp" />
        </shape>
    </item>
    <item>
        <shape android:shape="rectangle">
            <solid android:color="@color/colorEdittextBackground" />
            <corners android:radius="4dp" />
            <size
                android:width="50dp"
                android:height="50dp" />
        </shape>
    </item>
</layer-list>

0 个答案:

没有答案