我在SpannedText
中有EditText
,我正在用笑容替换文字,例如 cool 替换为笑脸。但是当我在Android手机(Nexus 5 kitkat 4.4.4)上按Backspace时,它会显示“(笑)酷”然后它会删除最后的字母微笑本身。但是在 Genymotion 模拟器上,它可以根据需要运行。我的代码出了什么问题?
此代码适用于
Drawable d = Drawable.createFromStream(ims, null);
Bitmap b = ((BitmapDrawable)d).getBitmap();
int spanSmileHeight = (displayHeight>displayWidth) ? ((int) ((double) displayHeight/25)) : ((int) ((double) displayWidth/25));
int spanSmileWidth = ((int) ((double) spanSmileHeight/b.getHeight())*b.getWidth())+15;
Bitmap bitmapResized = Bitmap.createScaledBitmap(b, spanSmileWidth, spanSmileHeight, false);
Drawable sd = new BitmapDrawable(getResources(), bitmapResized);
sd.setBounds(0, 0, spanSmileWidth, spanSmileHeight);
addPattern(emoticons, smileyCode, sd);
...
ImageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int start = Math.max(message.getSelectionStart(), 0);
int end = Math.max(message.getSelectionEnd(), 0);
message.getText().replace(Math.min(start, end), Math.max(start, end),
smileyCode, 0, smileyCode.length());
message.setText(getSmiledText(view.getContext(), message.getText()));
message.setSelection(start + smileyCode.length());
和这个
public static boolean addSmiles(Context context, Spannable spannable) {
boolean hasChanges = false;
for (Map.Entry<Pattern, Drawable> entry : emoticons.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(entry.getValue(), ImageSpan.ALIGN_BOTTOM),
matcher.start(), matcher.end(),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
}
}
return hasChanges;
}
public static Spannable getSmiledText(Context context, CharSequence text) {
Spannable spannable = spannableFactory.newSpannable(text);
addSmiles(context, spannable);
return spannable;
}
在bacspace之前
按下退格键(虚拟键盘)
日志输出:
09-01 06:22:21.280 1271-1271/com.nnadir.wapchat.app I/wapChat﹕ *sad**sad**sad**sad**sad**cat**cat*
09-01 06:23:32.060 1271-1271/com.nnadir.wapchat.app I/wapChat﹕ *sad**sad**sad**sad**sad**cat**cat
09-01 06:23:32.080 1271-1271/com.nnadir.wapchat.app I/wapChat﹕ *sad**sad**sad**sad**sad**cat**
09-01 06:23:32.084 1271-1271/com.nnadir.wapchat.app I/wapChat﹕ *sad**sad**sad**sad**sad**cat**cat
再多3次
09-01 06:24:07.980 1271-1271/com.nnadir.wapchat.app I/wapChat﹕ *sad**sad**sad**sad**sad**cat**ca
09-01 06:24:08.264 1271-1271/com.nnadir.wapchat.app I/wapChat﹕ *sad**sad**sad**sad**sad**cat**c
09-01 06:24:08.668 1271-1271/com.nnadir.wapchat.app I/wapChat﹕ *sad**sad**sad**sad**sad**cat**
此处微笑已删除
09-01 06:24:09.248 1271-1271/com.nnadir.wapchat.app I/wapChat﹕ *sad**sad**sad**sad**sad**cat*
答案 0 :(得分:0)
我找到了一种如何防止这种情况的方法。如果我们将targetSDK更改为15,则会成功删除微笑。