【发布时间】:2016-07-06 00:50:38
【问题描述】:
我有一个包含多个 EditText 项目的表单,每个项目都包装在 TextInputLayout 中。单击提交按钮时,我检查EditText 是否为空。如果是,我会在该字段上调用错误,例如ageWrapper.setError("Age required")。这会在该字段下显示一条错误消息,并将EditText 的hint 更改为红色。页面上的所有输入字段都会出现这种情况,并且错误运行正常。
在我清除错误后出现问题。我首先将该字段留空并提交 - 出现错误并且显示为红色。然后我在字段中输入一些内容,然后再次提交。现在,错误消失了 (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类:
@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;
}
}
【问题讨论】:
-
使用conditionWrapper.setError(null);
-
而不是
setErrorEnabled(false)?还是除此之外? -
而不是 setErrorEnabled(false) 放置此代码
-
完美,谢谢!如果您作为答案发布,我很乐意接受
标签: android android-layout android-edittext material-design android-textinputlayout