This is :) and want to :) replace with :D new image.
我有這種類型的字符串是我從EditTextbox.NOw得到了我想要全部替換「:)」和「:d」與image2.I想要做像string.replaceall(「:)」,image1)和string.replaceall(「:D」,image2)。所以任何人都可以建議我如何做到這一點與小代碼和更好的performance.I寫代碼,它工作也很好,但它需要很多時間。替換的字符,然後設置爲TextView的
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;
}
'這是工作也fine'所以究竟是什麼問題又來? – njzk2
我需要更好的解決方案,這將提高性能。這是工作,但需要很多時間bcz它會檢查字符的性格。所以我需要更好的解決方案 – Nency
似乎'setSpan'是最慢的部分...我有類似的與替換文字的背景顏色有關的問題。 – bancer