public class ProhibitEmojiUtil {
private ProhibitEmojiUtil() {
}
private static ProhibitEmojiUtil instance = null;
public static ProhibitEmojiUtil getInstance() {
if (instance == null) instance = new ProhibitEmojiUtil();
return instance;
}
public void setProhibitEmoji(EditText et) {
if (et != null) {
InputFilter[] filters = {getInputFilterProhibitEmoji(et.getContext())};
et.setFilters(filters);
}
}
public void setProhibitEmoji(EditText et, int maxlength) {
if (et != null) {
InputFilter[] filters = {getInputFilterProhibitEmoji(et.getContext()), new InputFilter.LengthFilter(maxlength)};
et.setFilters(filters);
}
}
private 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;
}
private InputFilter getInputFilterProhibitEmoji(final Context context) {
InputFilter filter = new InputFilter() {
@Override
public CharSequence filter(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 {
if (context != null) {
Toast.makeText(context, "请勿输入表情!", Toast.LENGTH_SHORT).show();
}
i++;
}
}
if (source instanceof Spanned) {
SpannableString sp = new SpannableString(buffer);
TextUtils.copySpansFrom((Spanned) source, start, end, null,
sp, 0);
return sp;
} else {
return buffer;
}
}
};
return filter;
}
}