我是Android移动开发的新手(Android Studio原生开发 - 用于新知识)。在这里,我想问一个关于输入验证的最佳实践的问题。 据我们所知,当开发人员开发输入表单时。我们需要阻止用户输入错误的输入文本字段。所以这是我的问题,
从我个人的角度来看,它应该有办法实现这项技术。因此,我们不需要为每个java文件重新使用相同的代码(根据干净的代码)。不幸的是,我没有找到任何示例或教程。也许我搜索错误的关键字或误读。如果不存在这样的技术,输入验证的最佳实践是什么?
谢谢。
p / s:这个帖子可以在最佳实践中找到更好的方法。谢谢。
答案 0 :(得分:15)
这个java类实现了TextWatcher
到" watch"编辑文本,观察对文本所做的任何更改:
public abstract class TextValidator implements TextWatcher {
private final TextView textView;
public TextValidator(TextView textView) {
this.textView = textView;
}
public abstract void validate(TextView textView, String text);
@Override
final public void afterTextChanged(Editable s) {
String text = textView.getText().toString();
validate(textView, text);
}
@Override
final public void
beforeTextChanged(CharSequence s, int start, int count, int after) {
/* Needs to be implemented, but we are not using it. */
}
@Override
final public void
onTextChanged(CharSequence s, int start, int before, int count) {
/* Needs to be implemented, but we are not using it. */
}
}
在EditText
中,您可以将该文本观察者设置为其监听器
editText.addTextChangedListener(new TextValidator(editText) {
@Override public void validate(TextView textView, String text) {
/* Insert your validation rules here */
}
});
答案 1 :(得分:6)
一种方法(我正在使用)是你应该有一个帮助来验证输入,例如:
这是我的ValidationHelper课程的一部分:
public class InputValidatorHelper {
public boolean isValidEmail(String string){
final String EMAIL_PATTERN = "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
Pattern pattern = Pattern.compile(EMAIL_PATTERN);
Matcher matcher = pattern.matcher(string);
return matcher.matches();
}
public boolean isValidPassword(String string, boolean allowSpecialChars){
String PATTERN;
if(allowSpecialChars){
//PATTERN = "((?=.*\\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%]).{6,20})";
PATTERN = "^[a-zA-Z@#$%]\\w{5,19}$";
}else{
//PATTERN = "((?=.*\\d)(?=.*[a-z])(?=.*[A-Z]).{6,20})";
PATTERN = "^[a-zA-Z]\\w{5,19}$";
}
Pattern pattern = Pattern.compile(PATTERN);
Matcher matcher = pattern.matcher(string);
return matcher.matches();
}
public boolean isNullOrEmpty(String string){
return TextUtils.isEmpty(string);
}
public boolean isNumeric(String string){
return TextUtils.isDigitsOnly(string);
}
//Add more validators here if necessary
}
现在我使用这个类的方式是:
InputValidatorHelper inputValidatorHelper = new InputValidatorHelper();
StringBuilder errMsg = new StringBuilder("Unable to save. Please fix the following errors and try again.\n");
//Validate and Save
boolean allowSave = true;
if (user.getEmail() == null && !inputValidatorHelper.isValidEmail(user_email)) {
errMsg.append("- Invalid email address.\n");
allowSave = false;
}
if (inputValidatorHelper.isNullOrEmpty(user_first_name)) {
errMsg.append("- First name should not be empty.\n");
allowSave = false;
}
if(allowSave){
//Proceed with your save logic here
}
您可以使用通过TextWatcher
EditText#addTextChangedListener
来调用您的验证
示例:
txtName.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
//Do nothing
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
validate();
}
});