我在xml中使用了TextInputLayout:
<android.support.design.widget.TextInputLayout
android:id="@+id/fname_label"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/TextLabel">
<com.app.sales_shfc.FontClass.Edittext_RobotoRegular
android:id="@+id/firstNameEt"
style="@style/editTextStylewhite"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:imeOptions="actionNext"
android:inputType="textPersonName"
android:maxLength="15" />
</android.support.design.widget.TextInputLayout>
并创建了一种将错误设置为textinputlayout的方法,
public static void setErrorTextInputLayout(TextInputLayout textInputLayout, String errorMsg, boolean enableError) {
textInputLayout.setErrorEnabled(enableError);
if (enableError) {
textInputLayout.setError(errorMsg);
}
}
我尝试设置setError null,但只要我的验证条件为false,就必须抛出错误
if (presalesValidation.first_name && (firstNameEt.getText().toString().trim().length() == 0 || !Utility.isNameValid(firstNameEt.getText().toString().trim()) || firstNameEt.getText().toString().contains(" "))) {
Utility.setErrorTextInputLayout(fname_label, "Please enter a proper first name", true);
validate = false;
} else {
Utility.setErrorTextInputLayout(fname_label, "", false);
}
第一次出现错误,但稍后会占用错误空间,但不会显示错误信息。
<style name="MyAppTheme.TextInputLayout" parent="@android:style/TextAppearance">
<item name="android:textColor">@color/errorRed</item>
</style>
这是主题:
<style name="TextLabel" parent="Widget.Design.TextInputLayout">
<!-- Hint color and label color in FALSE state -->
<item name="android:textColorHint">@color/secondaryColor</item>
<item name="android:textSize">20sp</item>
<item name="android:backgroundTint">@color/secondaryColor</item>
<!-- Label color in TRUE state and bar color FALSE and TRUE State -->
<item name="colorAccent">@color/secondaryColor</item>
<item name="colorControlNormal">@color/secondaryColor</item>
<item name="colorControlActivated">@color/secondaryColor</item>
<item name="errorTextAppearance">@style/MyAppTheme.TextInputLayout</item>
</style>
答案 0 :(得分:0)
尝试请求在设置错误后关注相同的EditText
public static void setErrorTextInputLayout(TextInputLayout textInputLayout, String errorMsg, boolean enableError) {
textInputLayout.setErrorEnabled(enableError);
if (enableError) {
textInputLayout.setError(errorMsg);
textInputLayout.requestFocus();
}
}