public class EmojiUtils {
//限制输入表情
public static void setProhibitEmoji(EditText et) {
InputFilter[] filters = {getInputFilterProhibitEmoji()};
et.setFilters(filters);
}
//限制输入表情,并且限制输入长度
public static void setProhibitEmojilength(EditText et,int length) {
InputFilter[] filters = {new InputFilter.LengthFilter(length), getInputFilterProhibitEmoji()};
et.setFilters(filters);
}
public static InputFiltergetInputFilterProhibitEmoji() {
InputFilter filter =new InputFilter() {
@Override
public CharSequencefilter(CharSequence source, int start, int end,
Spanned dest, int dstart, int dend) {
StringBuffer buffer =new StringBuffer();
for (int i = start; i < end; i++) {
char codePoint = source.charAt(i);
if (!getIsEmoji(codePoint)) {
buffer.append(codePoint);
}else {
i++;
continue;
}
}
if (sourceinstanceof Spanned) {
SpannableString sp =new SpannableString(buffer);
TextUtils.copySpansFrom((Spanned) source, start, end, null,
sp, 0);
return sp;
}else {
return buffer;
}
}
};
return filter;
}
public static boolean getIsEmoji(char codePoint) {
if ((codePoint ==0x0) || (codePoint ==0x9) || (codePoint ==0xA)
|| (codePoint ==0xD)
|| ((codePoint >=0x20) && (codePoint <=0xD7FF))
|| ((codePoint >=0xE000) && (codePoint <=0xFFFD))
|| ((codePoint >=0x10000) && (codePoint <=0x10FFFF)))
return false;
return true;
}
}
调用方法
editText.setProhibitEmoji(et);
或者
editText.setProhibitEmoji(et,5);
网友评论