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

标签 android android-layout android-edittext material-design android-textinputlayout

我有一个包含多个 EditText 项的表单,每个项都包含在 TextInputLayout 中。单击“提交”按钮时,我检查 EditText 是否为空。如果是,我会在该字段上调用错误,例如ageWrapper.setError("年龄要求")。这会在字段下显示一条错误消息,并将 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>

fragment 的 Java 类:

@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);
        }

关于清除错误后,Android TextInputLayout 保持红色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36098398/

相关文章:

java - 在 Android 中移动可绘制对象时遇到问题

java - EditText 到 String 返回 Null

android - RTL 支持可绘制左侧的自定义编辑文本

android - 膨胀内部类 View 时出错

android - 动态 RecyclerView ListView 下方的 LinearLayout

Android 相当于 : void main()/Sub Main?

java - 如何为 Android 上的图像按钮设置可见性动画?

Android 软件键盘触发快速搜索栏

Android Studio 未将应用程序安装到设备中

android - 我什么时候需要基础 Activity 和基础 fragment ?