我把这段代码放在了答案Displaying emoticons in Android for show emoticons in Edittext 中,但是当我输入大量文本时出现问题,它变得越来越慢,几乎无法输入。这是代码:我怎样才能让它更快?
textS.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void afterTextChanged(Editable s) {
addSmiledText(Main.this, s);
// Log.e("",s.toString());
}
});}
private static final HashMap<String, Integer> emoticons = new HashMap<String, Integer>();
static {
emoticons.put(":)", R.drawable.face_smile);
emoticons.put(":-)", R.drawable.face_smile);
emoticons.put(":(", R.drawable.face_sad);
emoticons.put(":-(", R.drawable.face_sad);
emoticons.put(":-D", R.drawable.face_smile_big);
emoticons.put(":D", R.drawable.face_smile_big);
emoticons.put(":lol:", R.drawable.face_laughing);
emoticons.put("8)", R.drawable.face_cool);
emoticons.put(";)", R.drawable.face_wink);
emoticons.put(";-)", R.drawable.face_wink);
emoticons.put(";(", R.drawable.face_crying);
...
}
public static Spannable addSmiledText(Context ch, Editable s) {
int index;
for (index = 0; index < s.length(); index++) {
for (Entry<String, Integer> entry : emoticons.entrySet()) {
int length = entry.getKey().length();
if (index + length > s.length())
continue;
if (s.subSequence(index, index + length).toString().equals(entry.getKey())) {
s.setSpan(new ImageSpan(ch, entry.getValue()), index, index + length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
index += length - 1;
break;
}
}
}
return s;
}