TextInputLayout 错误颜色未清除?

TextInputLayout error color not getting cleared?

我有一个 TextInputLayout,里面有一个 EditText。

这是我的 xml:

<android.support.design.widget.TextInputLayout
    android:id="@+id/textInputLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <EditText
        android:id="@+id/editText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="Enter Text" />

</android.support.design.widget.TextInputLayout>

我的java代码:

((TextInputLayout) findViewById(R.id.textInputLayout)).setError("ERROR");

当我调用 setError("ERROR") 时,标签(提示)颜色和 EditText 的底线颜色变为红色并出现错误。这是我期望的行为。

现在假设我在销毁 activity 之前没有调用 setError(null)。现在我再次打开相同的 activity。我可以看到我的应用程序中所有 EditText 字段的底线仍然是红色,尽管标签颜色似乎已重置并且错误消息已被消除。这并不总是可重现的,但如果我继续尝试,我最终会得到它。

我正在使用带有 5.1.1 的 Nexus 4。

我是不是做错了什么?

这是由于 AppCompat 库中的错误

Reported by elyess.a...@gmail.com, Oct 19, 2015 Using design support library 23.1.0

Steps to reproduce the problem (including sample code if appropriate).

  • SetError on one TIL (i.e. in a form)
  • The TIL has a red underline (ok)
  • Navigate back and enter the activity again. Or go to another Activity with TILs.

What happened.

  • All the TILs have a red underline, even in other Activities. (but no error text).
  • The red underlines disappear only after force closing the app.

这里也有报道:


问题状态已于 2015 年 11 月 11 日更改为 FutureRelease,因此我们希望尽快修复。

同时,似乎有 3 个解决方法:

此问题已在 com.android.support 的 23.1.1 版中解决:...库

遇到同样的问题,对我有用的是将我的主题更改为从 Theme.Design.* 扩展。 资料来源:Issue 202051: UnsupportedOperationException in TextInputLayout's counter

正如@Richard 所说,这是一个错误。 Issue 190829: TextInputLayout setError causes all TILs in the app to have red underline

我已经使用了将常态设置回后台的解决方案。您可以使用自己的自定义 class 扩展 TextInputLayout,您可以在其中覆盖 setError() 方法:

public class CustomTextInputLayout extends TextInputLayout {

    // Constructors...

    @Override
    public void setError(@Nullable CharSequence error) {

        super.setError(error);
        if ((getEditText() != null && getEditText().getBackground() != null) &&
            (Build.VERSION.SDK_INT == 22 || Build.VERSION.SDK_INT == 21)) {
            Drawable drawable = getEditText().getBackground().getConstantState().newDrawable();
            getEditText().setBackgroundDrawable(drawable);
        }
    }
}

然后我重新使用这个 class 来包装 EditText。我没有遇到任何副作用。