0
我有一个EditText
用户必须输入电话号码。电话号码的格式为:^5[4][0-9]{10}$
第一个号码是5
,第二个号码是4
后跟10位数字。Android验证输入,而用户输入
我用下面InputFilter
InputFilter filter= new InputFilter() {
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
for (int i = start; i < end; i++) {
String checkMe = String.valueOf(source.charAt(i));
Pattern pattern = Pattern.compile("^5[4][0-9]{10}$");
Matcher matcher = pattern.matcher(checkMe);
boolean valid = matcher.matches();
if(!valid){
return "";
}
}
return null;
}
};
尝试,但这个只有充分号相匹配,我想在用户输入数字来验证。
想象一下这样的情况:
User enters 6 -> Does not match the initial digit of the pattern so EditText stays empty
User enters 5 -> Matches the first digit of the pattern so EditText text = 5
User enters 3 -> Does not match the second digit of the pattern so EditText text = 5
User enters 4 -> Matches the second digit of the pattern so EditText text = 54
From now, if the user adds a digit, EditText will append that until the length of 10
任何想法,我怎么能做到这一点?
是的。谢谢。解决了。 – Favolas