This is :) and want to :) replace with :D new image.
EditTextbox から取得したこのタイプの文字列があります。今、すべての「:)」を image1 に、「:D」を image2 に置き換えたいと思います。string.replaceall(":)",image1) のようにしたいと string.replaceall(":D",image2).だから、小さなコードでこれを行う方法とパフォーマンスを向上させる方法を誰かに提案してもらえますか?
textview.setText(getSmiledText(ctx, stringvalue));
private static final HashMap<String, Integer> emoticons = new HashMap<String, Integer>();
static {
emoticons.put(":)", R.drawable.j1);
emoticons.put(":D", R.drawable.j2);}
public static Spannable getSmiledText(Context context, String s) {
int index;
SpannableStringBuilder builder = new SpannableStringBuilder();
builder.append(s);
for (index = 0; index < builder.length(); index++) {
for (Entry<String, Integer> entry : emoticons.entrySet()) {
int length = entry.getKey().length();
if (index + length > builder.length())
continue;
if (builder.subSequence(index, index + length).toString()
.equals(entry.getKey())) {
builder.setSpan(new ImageSpan(context, entry.getValue()),
index, index + length,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
index += length - 1;
break;
}
}
}
return builder;
}