1. 现在网上搜一大堆使用正则匹配屏蔽表情输入的方式,代码无外乎就是下面这种:
InputFilter emojiFilter = new InputFilter() {
Pattern emoji = Pattern.compile("[\ud83c\udc00-\ud83c\udfff]|[\ud83d\udc00-\ud83d\udfff]|[\u2600-\u27ff]",
Pattern.UNICODE_CASE | Pattern.CASE_INSENSITIVE);
@Override
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
Matcher emojiMatcher = emoji.matcher(source);
if (emojiMatcher.find()) {
Toast.makeText(MainActivity.this,"不支持输入表情", 0).show();
return "";
}
return null;
}
};
这种方法只能屏蔽日常的表情,如果表情比较新颖则无法屏蔽,我这边通过获取无法屏蔽表情的Unicode编码值,完美的解决了比较新颖的表情不能屏蔽的问题,代码如下:
InputFilter emojiFilter = new InputFilter() {
Pattern emoji = Pattern.compile("[\ud83c\udc00-\ud83c\udfff]|[\ud83d\udc00-\ud83d\udfff]|[\\ud83e\\udc00-\\ud83e\\udfff]||[\u2600-\u27ff]",Pattern.UNICODE_CASE | Pattern.CASE_INSENSITIVE);
@Override
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
Matcher emojiMatcher = emoji.matcher(source);
if (emojiMatcher.find()) {
Toast.makeText(MainActivity.this,"不支持输入表情", 0).show();
return "";
}
return null;
}
};
重点就是加入了[\\ud83e\\udc00-\\ud83e\\udfff]正则表达式,这个Unicode字符范围内都是比较新颖的表情的值区间。就是这么简单。