我有一个聊天应用程序,可以添加表情符号到文本.
我有一个EditText字段的问题.表情符号图像显示,但如果我按正常的键盘退格按钮,我正在更改为表情符号图片的文本显示,我必须删除几个字符,直到图片消失.我正在使用Spannable来做到这一点.
如果用户按下一次退格键,我想让整个傻瓜走开.
这里我使用的代码:
// This is in the keyclicked listener { ... smilie = "(angel)"; break; ... int cursorPosition = content.getSelectionStart(); content.getText().insert(cursorPosition,getSmiledText(this,smilie)); content.getText().insert(cursorPosition + smilie.length()," "); } public static boolean addSmiles(Context context,Spannable spannable) { boolean hasChanges = false; for (Entry<Pattern,Integer> entry : smilies.entrySet()) { Matcher matcher = entry.getKey().matcher(spannable); while (matcher.find()) { boolean set = true; for (ImageSpan span : spannable.getSpans(matcher.start(),matcher.end(),ImageSpan.class)) if (spannable.getSpanStart(span) >= matcher.start() && spannable.getSpanEnd(span) <= matcher.end()) spannable.removeSpan(span); else { set = false; break; } if (set) { hasChanges = true; spannable.setSpan(new ImageSpan(context,entry.getValue()),matcher.start(),Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); } } } return hasChanges; } public static Spannable getSmiledText(Context context,CharSequence text) { Spannable spannable = spannableFactory.newSpannable(text); addSmiles(context,spannable); return spannable; }