/**
* 禁止输入表情以及特殊字符
*/
public static class EmojiExcludeFilter implements InputFilter {
@Override
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
for (int i = start; i < end; i++) {
int type = Character.getType(source.charAt(i));
if (type == Character.SURROGATE || type == Character.OTHER_SYMBOL) {
return "";
}
}
String speChat = "[`~!@#$%^&*()+=|{}':;'\\[\\].<>/?~!@#¥%……&*()——+|{}【】‘”“’?]";
Pattern pattern = Pattern.compile(speChat);
Matcher matcher = pattern.matcher(source.toString());
if (matcher.find()) {
return "";
} else {
return null;
}
}
}
调用如下:
edtRemark.setFilters(new InputFilter[]{new UIHelper.EmojiExcludeFilter()});