TextInputLayout在将setError属性设置为null后会删除EditText的样式

5

我想在我的新应用程序中使用TextInputLayout。我有这样的布局

***
    <android.support.design.widget.TextInputLayout
        android:id="@+id/input_layout_email"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:textColorHint="@color/text_color"
        app:hintTextAppearance="@style/HintTextAppearance.TextInputLayout"
        app:errorTextAppearance="@style/ErrorTextAppearance.TextInputLayout">

        <EditText
            android:id="@+id/input_email"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="textEmailAddress"
            android:hint="@string/hint_email"
            android:background="@drawable/edit_text_border_radius"
            android:padding="10dp"
            android:drawableLeft="@drawable/ic_acc"/>
    </android.support.design.widget.TextInputLayout>
 ***

在我的活动中,我有以下验证:
    private boolean validatePassword() {
            if (inputPassword.getText().toString().trim().isEmpty()) {
                inputLayoutPassword.setError(getString(R.string.err_msg_password));
                requestFocus(inputPassword);
                return false;
            } else {
                inputLayoutPassword.setError(null);// it removes @drawable/edit_text_border_radius style from EditText
                inputLayoutPassword.setErrorEnabled(false);      
            }

            return true;
     }

虽然它工作正常,但如果您注意到我已经为EditText声明了@drawable/edit_text_border_radius资源。如果第一次我不填写密码字段,它将更改其背景颜色为红色。因为这是TextInputLayout错误跨度的默认颜色。但是,如果我用某些值填充同一字段,那么红色错误跨度就会消失,但EditText元素却忘记了在之前设置给它的背景资源(@drawable/edit_text_border_radius)。


@Sree,抱歉,您指的是哪些颜色? - AEMLoviji
我的意思是红色和另一个。 - Sree
红色错误提示框来自TextInputLayout的标准颜色。在调用setError(null)之后出现的另一种颜色我无法理解它从哪里来。 - AEMLoviji
所以最好为文本和背景设置颜色,无论您想要什么,如果您发现一个条目。 - Sree
让我们在聊天中继续这个讨论 - AEMLoviji
显示剩余4条评论
1个回答

2

不确定您是否已经找到解决方案,但我刚遇到了同样的问题。

深入研究TextInputLayout源代码,特别是清除错误消息的逻辑,看起来像是EditText的背景色过滤器被清除了(在我的情况下,它变成了黑色)。

目前我想到的快速而简单的解决方案就是手动将背景过滤器重置为所需颜色:

private boolean validatePassword() {
    if (inputPassword.getText().toString().trim().isEmpty()) {
        inputLayoutPassword.setError(getString(R.string.err_msg_password));
        requestFocus(inputPassword);
        return false;
    } else {
        inputLayoutPassword.setError(null);// it removes @drawable/edit_text_border_radius style from EditText
        inputLayoutPassword.setErrorEnabled(false);

        // manually resetting the background color filter of edit text
        if(inputLayoutPassword.getEditText() != null) {
            if(inputLayoutPassword.getEditText().getBackground() != null) {
                inputLayoutPassword.getEditText()
                    .getBackground()
                    .setColorFilter(
                        ContextCompat.getColor(getActivity(), R.color.some_color),
                        PorterDuff.Mode.SRC_IN
                    );
            }
        }
    }

    return true;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接