Android TextInputLayout 在清除错误后保持红色

Android TextInputLayout remains red after clearing error

我有一个包含多个 EditText 项的表单,每个项都包含在 TextInputLayout 中。单击“提交”按钮时,我检查 EditText 是否为空。如果是,我会在该字段上调用错误,例如ageWrapper.setError("Age required")。这会在字段下显示一条错误消息,并将 EditTexthint 更改为红色。页面上的所有输入字段都会出现这种情况,错误正常。

清除错误后出现问题。我首先将该字段留空并提交 - 出现错误并且显示为红色。然后我在字段中输入一些内容,然后再次提交。现在,错误消失了(ageWrapper.setErrorEnabled(false)),但是 hint 仍然是红色,而不是变回正常的、没有错误的颜色。

这不应该发生,因为错误已通过在字段中键入内容清除,但提示仍然是红色,暗示错误,即使错误消息本身(在字段下)消失了。

当我再次单击该字段或页面上的任何其他字段时,红色提示文本会变回其正常颜色。

很明显,错误显示正在工作 - 它显示并按应有的方式消失。但是,为什么在我通过在字段中键入内容清除错误后提示文本仍保持红色,并且只有在单击该字段本身(或另一个字段)后才变回其常规颜色?我如何摆脱这种行为?

在我看来,如果两个字段都是空的,两者都会显示红色并显示错误消息。如果我随后填写其中一个字段,则应该在该字段上设置 setErrorEnabled(false) 并且提示文本应该恢复正常,但事实并非如此。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="@dimen/new_layout_padding">

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

        <EditText
            android:id="@+id/input_condition"
            android:layout_width="match_parent"
            android:layout_height="@dimen/new_form_element_height"
            android:hint="@string/input_text_condition"
            android:inputType="number" />
    </android.support.design.widget.TextInputLayout>

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

        <EditText
            android:id="@+id/input_age"
            android:layout_width="match_parent"
            android:layout_height="@dimen/new_form_element_height"
            android:hint="@string/input_text_age"
            android:inputType="number" />
    </android.support.design.widget.TextInputLayout>

    <Button
        android:id="@+id/input_submit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:ems="5"
        android:text="@string/input_button_save" />

</RelativeLayout>

Java class 片段:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.fragment_new, container, false);

    //Get all text fields
    conditionWrapper = (TextInputLayout) view.findViewById(R.id.input_condition_wrapper);
    ageWrapper = (TextInputLayout) view.findViewById(R.id.input_age_wrapper);

    //Listener for create button
    createButton = (Button) view.findViewById(R.id.input_submit);
    createButton.setOnClickListener(this);

    // Inflate the layout for this fragment
    return view;
}

//Create/submit button click
@Override
public void onClick(View v) {
    //Get input values
    String condition = conditionWrapper.getEditText().getText().toString();
    String age = ageWrapper.getEditText().getText().toString();

    //If all the validation passes, submit the form. Else, show errors
    if (!isEmpty(condition) & !isEmpty(age)) {
        //Submit form data
    } else {
        if (isEmpty(condition)) {
            conditionWrapper.setError("Condition required");
        } else {
            conditionWrapper.setErrorEnabled(false);
        }

        if (isEmpty(age)) {
            ageWrapper.setError("Age required");
        } else {
            ageWrapper.setErrorEnabled(false);
        }
    }
}

//Check if a string is empty
public boolean isEmpty(String string) {
    if (string.equals("")) {
        return true;
    } else {
        return false;
    }
}

将您的代码更改为

 if (isEmpty(condition)) {
            conditionWrapper.setError("Condition required");
        } else {
             conditionWrapper.setError(null);
        }