android非空等格式驗證框架android-saripaar地址

dingcheng998發表於2016-11-19

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


步驟1:使用註解

@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 = "You must agree to the terms.")
private CheckBox iAgreeCheckBox;

步驟2:監聽驗證

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Code…

Validator validator = new Validator(this);
validator.setValidationListener(this); // More code…}


步驟3:實現監聽介面:

public class RegistrationActivity extends Activity implements ValidationListener {

    // Code…

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

步驟4:驗證結果 註冊按鈕

registerButton.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        validator.validate();
    }
});

登入驗證示列:

@Length(min = 6,message = "賬號不能少於6位")
    private EditText userName;
    @Length(min = 8, max = 16,message = "密碼不能少於8位或大於16位")
    private EditText passWord;

 private Validator validator;

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

 @Override
    public void onValidationSucceeded() {
                removeVerify();
    }

    @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 TextInputEditText) {
                ((TextInputEditText) view).setError(message);
            } else {
                Toast.makeText(this, message, Toast.LENGTH_LONG).show();
            }
        }
    }


 case R.id.sure_login:
                validator.validate();
                break;


相關文章