android中Edittext校验库

## 官网搬运

https://github.com/ragunathjawahar/android-saripaar

## 导入

dependencies {
    compile 'com.mobsandgeeks:android-saripaar:2.0.3'
}
allprojects {
    repositories {
        jcenter()
        maven {
            url "https://oss.sonatype.org/content/repositories/snapshots/"
        }
    }
}

## 混淆

-keep class com.mobsandgeeks.saripaar.** {*;}
-keep @com.mobsandgeeks.saripaar.annotation.ValidateUsing class * {*;}

## 使用

### 第一步
@NotEmpty
@Email
private EditText emailEditText;

@Password(min = 6, scheme = Password.Scheme.ALPHA_NUMERIC_MIXED_CASE_SYMBOLS)
private EditText passwordEditText;

@ConfirmPassword
private EditText confirmPasswordEditText;

@Checked(message = "${提示语句}")
private CheckBox iAgreeCheckBox;
### 第二步
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Code…

    validator = new Validator(this);
    validator.setValidationListener(this);

    // More code…
}
### 第三步
implements ValidationListener

    @Override
    public void onValidationSucceeded() {
        Toast.makeText(this, "Yay! we got it right!",     
        Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onValidationFailed(List<ValidationError> errors) {
        for (ValidationError error : errors) {
            View view = error.getView();
            String message = error.getCollatedErrorMessage(this);

            // Display error messages ;)
            if (view instanceof EditText) {
                ((EditText) view).setError(message);
            } else {
                Toast.makeText(this, message, 
                Toast.LENGTH_LONG).show();
            }
        }
    }
### 第四步 在提交的地方验证一波就完了
validator.validate();

 

posted @ 2021-05-19 09:46  码弄  阅读(198)  评论(0编辑  收藏  举报