我是android移动开发的新手(Android本地开发-为了新的知识)。在这里,我想问一个关于输入验证的最佳实践的问题。据我们所知,当开发人员开发输入表单时。我们需要防止用户将错误的输入输入到文本字段。所以这是我的问题
在我个人看来,它应该有一种方法来实现这一技术。这样,我们就不需要对每个java文件(从干净代码的角度)再次重复使用相同的代码。不幸的是,我没有找到任何的例子或教程。也许我搜索错了关键词或者误读了。如果不存在这样的技术,那么输入验证的最佳实践是什么?
谢谢。
p/s:在最佳实践中寻找一个更好的方法。谢谢。
发布于 2015-10-12 03:00:47
这个java类实现了一个TextWatcher来“监视”您的编辑文本,观察对文本所做的任何更改:
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 */
}
});发布于 2015-10-12 03:06:48
有一种方法(我正在使用)是,您应该有一个用于验证输入的助手,如:
下面是我的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();
}
});https://stackoverflow.com/questions/33072569
复制相似问题