/**
* 限制edittext 不能输入中文
* @param editText
*/
public static void setEdNoChinaese(final EditText editText){
TextWatcher textWatcher=new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
String txt = s.toString();
//注意返回值是char数组
char[] stringArr = txt.toCharArray();
for (int i = 0; i < stringArr.length; i++) {
//转化为string
String value = new String(String.valueOf(stringArr[i]));
Pattern p = Pattern.compile("[\u4e00-\u9fa5]");
Matcher m = p.matcher(value);
if (m.matches()) {
editText.setText(editText.getText().toString().substring(0, editText.getText().toString().length() - 1));
editText.setSelection(editText.getText().toString().length());
return;
}
}
}
@Override
public void afterTextChanged(Editable s) {
}
};
editText.addTextChangedListener(textWatcher);
}
限制EditText 不能输入中文
最新推荐文章于 2024-07-08 03:07:36 发布