有人可以解释一下我如何使用复合设计模式来验证android中的注册表单。在这个表单中,我有 8 个 EditText 字段(用户名、密码、密码重新输入、城市、性别、年龄、电话号码、电子邮件)。我已经使用此代码对其进行了验证
public boolean validateInput() {
boolean validated = true;
if (mUserValues.getCity() == null || mUserValues.getCity().isEmpty()) {
mCityEditText.setError(getString(R.string.error));
validated = false;
}
if (mUserValues.getEmail() == null || mUserValues.getEmail().isEmpty()) {
Log.d("mEmail", "in mEmail");
mEmail.setError(getString(R.string.error));
validated = false;
}
if (mUserValues.getPassword() == null
|| mUserValues.getPassword().isEmpty()) {
mPasswordEditText.setError(getString(R.string.error));
validated = false;
}
if (mUserValues.getPasswordCheck() == null
|| mUserValues.getPasswordCheck().isEmpty()) {
mPasswordConfirm.setError(getString(R.string.error));
validated = false;
}
if (mUserValues.getPassword() != null
&& mUserValues.getPasswordCheck() != null
&& !mPasswordEditText.getText().toString()
.equals(mPasswordEditText.getText().toString())) {
mPasswordEditText.setError(getString(R.string.pass_check));
mPasswordConfirm.setError(getString(R.string.pass_check));
validated = false;
}
if (mUserValues.getSex() == null || mUserValues.getSex().isEmpty()) {
mSex.setError(getString(R.string.error));
validated = false;
}
if (mUserValues.getPhoneNumber() == 0) {
mPhone.setError(getString(R.string.error));
validated = false;
}
Pattern pattern = Pattern.compile(Constants.EMAIL_PATTERN);
Matcher matcher = pattern.matcher(mEmail.getText().toString());
mEmailCheck = matcher.matches();
if (mEmailCheck == false) {
mEmail.setError(getString(R.string.email_valid));
validated = false;
}
if (mUserValues.getUsername().length() <= 2) {
mUsername.setError(getString(R.string.username_check));
validated = false;
}
if ((mUserValues.getAge() < 18 || mUserValues.getAge() > 150)
&& mUserValues.getAge() > 0) {
mAge.setError(getString(R.string.age_check));
validated = false;
}
return validated;
}
我的一个朋友告诉我,它可以使用复合设计模式来完成,但我真的不明白如何做到这一点。如果有人可以解释,我将不胜感激。提前致谢。