原创

Android表单校验以及错误信息提示

温馨提示:
本文最后更新于 2020年11月04日,已超过 1,479 天没有更新。若文章内的图片失效(无法正常加载),请留言反馈或直接联系我

两种显示错误信息的方式

  1. 直接在EditView中显示错误信息,但是会有一个图标,需要点击图标才能显示错误信息,不太直观。
  2. 直接在EditView的Layout布局中显示错误信息,比较直观。

xml布局代码:

<com.google.android.material.textfield.TextInputLayout
    android:id="@+id/phaseDifferenceLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="@dimen/activity_horizontal_margin">
    <com.google.android.material.textfield.TextInputEditText
        android:id="@+id/phaseDifference"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="numberDecimal"
        android:hint="相间差" />
</com.google.android.material.textfield.TextInputLayout>

Java代码:

// 监听validation的变化
private void validateData() {
    windingTaskViewModel = ViewModelProviders.of(this).get(WindingTaskViewModel.class);
    windingTaskViewModel.getValidation().observe(this, validation -> {
        //            binding.saveButton.setEnabled(validation.isValid());
        if (!validation.isValid()) {
            // 1.设置EditView的Error
            FormValidation.ErrorDetail errorDetail = validation.getError();
            errorDetail.getTextView().setError(getString(errorDetail.getErrorId()));
            
            // 2.设置TextInputLayout的Error
            errorDetail.getTextInputLayout().setErrorEnabled(true);
            errorDetail.getTextInputLayout().setError(getString(errorDetail.getErrorId()));
        }
    });
}
// 校验
validateData();

// 将EditView和EditViewLayout传入进去
windingTaskViewModel.validate(binding.phaseDifferenceLayout, binding.phaseDifference);

校验:

@Getter
private final MutableLiveData<FormValidation> validation = new MutableLiveData<>();

// ......

// 在我的这里,相间差phaseDifference是自动计算的,为了演示我就给出主要的代码
// 假如phaseDifference大于0.02,就提示错误信息
public void validate(TextInputLayout phaseDifferencetLayout, EditText phaseDifferenceView) {
    if (phaseDifferenceView.getText().toString().isEmpty()) {
        return;
    }

    isValid = true;
    BigDecimal phaseDifference = new BigDecimal(phaseDifferenceView.getText().toString());
    BigDecimal standard = new BigDecimal("0.02");

    if (phaseDifference.compareTo(standard) > 0) {
        isValid = false;
        validation.setValue(new FormValidation(phaseDifferencetLayout,
                                               phaseDifferenceView, R.string.phaseDifferenceWarning));
    }

    Log.i(TAG, "isValid:" +isValid);
    if (isValid) {
        phaseDifferencetLayout.setErrorEnabled(false);
        phaseDifferenceView.setError(null);
        validation.setValue(new FormValidation(true));
    }
}

错误信息:

@Getter
@ToString
public class FormValidation {

    private final boolean isValid;
    private ErrorDetail error;

    public FormValidation(TextInputLayout textInputLayout, TextView textView, Integer error) {
        this.isValid = false;
        this.error = new ErrorDetail(textInputLayout, textView, error);
    }

    public FormValidation(boolean isValid) {
        this.isValid = isValid;
    }

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    @ToString
    public static class ErrorDetail {
        private TextInputLayout textInputLayout;
        private TextView textView;
        private Integer errorId;
    }
}

当验证不通过:

Android的错误信息提示.png

当验证通过:

Android的错误信息提示-验证通过.png

本文目录