1. 現(xiàn)在網(wǎng)上搜一大堆使用正則匹配屏蔽表情輸入的方式枫耳,代碼無(wú)外乎就是下面這種:
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;
}
};
這種方法只能屏蔽日常的表情叉抡,如果表情比較新穎則無(wú)法屏蔽论皆,我這邊通過(guò)獲取無(wú)法屏蔽表情的Unicode編碼值旧乞,完美的解決了比較新穎的表情不能屏蔽的問(wèn)題沸毁,代碼如下:
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;
}
};
重點(diǎn)就是加入了[\\ud83e\\udc00-\\ud83e\\udfff]正則表達(dá)式摩桶,這個(gè)Unicode字符范圍內(nèi)都是比較新穎的表情的值區(qū)間。就是這么簡(jiǎn)單犀暑。