import android.text.InputFilter; import android.text.SpannableString; import android.text.Spanned; import android.text.TextUtils; import android.widget.EditText; public class EmojiUtils { //限制输入表情 public static void setProhibitEmoji(EditText et) { InputFilter[] filters = {getInputFilterProhibitEmoji()}; et.setFilters(filters); } public static InputFilter getInputFilterProhibitEmoji() { 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 { i++; continue; } } 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; } 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; } //限制输入表情,并且限制输入字数 public static void setProhibitEmojilength(EditText et,int length) { InputFilter[] filters = {new InputFilter.LengthFilter(length), getInputFilterProhibitEmoji()}; et.setFilters(filters); } }
EditText限制输入表情,限制输入字数长度
最新推荐文章于 2022-11-23 14:42:26 发布