输入框的内容如何验证只能输汉字,或者数字,或者数字与字母的组合?
[a-zA-Z\u4e00-\u9fa5][a-zA-Z0-9\u4e00-\u9fa5]+
使用正则表达式
汉字的范围是\u4e00-\u9fa5
有些可以通过EditText的inputType属性设置,比如number,其他的可以代码中监听文本变化,然后用正则表达式适配。匹配双字节字符(包括中文在内):[^\x00-\xff]。其他的一楼已回答。
用TextWatch监听字符变化,然后用这个判断是不是中文
String txt = edInput.getText().toString();
Pattern p = Pattern.compile("[0-9]*");
Matcher m = p.matcher(txt);
if(m.matches() ){
Toast.makeText(Main.this,"输入的是数字", Toast.LENGTH_SHORT).show();
}
p=Pattern.compile("[a-zA-Z]");
m=p.matcher(txt);
if(m.matches()){
Toast.makeText(Main.this,"输入的是字母", Toast.LENGTH_SHORT).show();
}
p=Pattern.compile("[\u4e00-\u9fa5]");
m=p.matcher(txt);
if(m.matches()){
Toast.makeText(Main.this,"输入的是汉字", Toast.LENGTH_SHORT).show();
}
一般使用正则 或者 inputType 属性