/**
* 设置EditText是否过滤汉字
*/
public static void SetEditTextFilter(EditText editText, final boolean filterChinese) {
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 (filterChinese == (isChinese(source.charAt(i)))) {
return "";
}
}
return null;
}
};
editText.setFilters(new InputFilter[]{filter});
}
/**
* 判断输入字符是否是汉字
*
* @param c 输入字符
* @return 是否是汉字
*/
public static boolean isChinese(char c) {
Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
return ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS
|| ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS
|| ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A
|| ub == Character.UnicodeBlock.GENERAL_PUNCTUATION
|| ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION
|| ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS;
}
Android 代码设置EditText过滤汉字
最新推荐文章于 2022-01-19 09:40:37 发布