/** * edittext输入监听,不允许输入中文 */ public class NoChineseTextWatcher implements TextWatcher { final String reg = "[^[\u4E00-\u9FA5]]";//正则表达式,非中文 private boolean isNotMatch = false; private EditText editText; public NoChineseTextWatcher(EditText editText){ this.editText = editText; } @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override public void onTextChanged(CharSequence s, int start, int before, int count) { } @Override public void afterTextChanged(Editable s) { String str = s.toString(); if (!TextUtils.isEmpty(str)){ char[] chars = str.toCharArray(); for (int i = 0; i < str.length(); i++) { String aChar = String.valueOf(chars[i]); if (!aChar.matches(reg)) { isNotMatch = true; } } if (isNotMatch){ ToastUtil.showToast("密码不能设置中文,请重新设置!"); editText.setText(""); isNotMatch = false; } }else { isNotMatch = false; } } }
限制EditText不能输入中文的监听器
最新推荐文章于 2021-05-28 14:52:46 发布