我的应用程序将用户的userid作为输入,userid是字母数字,即只有第一个字符是(a-z),其他部分是数字。我如何验证此类型的输入(如G34555)?如何检查在EditText中输入的值是否为aplhanumeric?
2
A
回答
0
我已经解决了问题,通过使用简单的string
功能matches
String str="mystring";
str.matches("[a-zA-Z][0-9]+");
3
使用正则表达式。这应该这样做假设的第一个字母可以为大写或小写:
Pattern p = Pattern.compile("[a-zA-Z][0-9]+");
Matcher m = p.matcher("some text you want");
boolean isAlphaNum = m.matches();
3
http://osdir.com/ml/Android-Developers/2009-11/msg02501.html似乎是一个更体面的解决方案,它不允许输入不被接受的字符。从链接
代码:
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++) {
if (!Character.isLetterOrDigit(source.charAt(i))) {
return "";
}
}
return null;
}
};
edit.setFilters(new InputFilter[]{filter});
这是行不通的,从未返回true。有什么建议么? – rob 2010-04-18 20:36:29