Я использую TextWatcher
изменить EditText
всегда выглядеть #tag1 #tag2...
TextWatcher медленно при использовании SetText
Вот мой код:
editHashtags.addTextChangedListener(new TextWatcher() {
private boolean lock = false;
private String toGo;
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if(before == 1) return;
toGo = s.toString();
if(toGo.charAt(0) != '#') {
toGo = '#' + toGo;
}
if(toGo.charAt(start) == ' ' && toGo.charAt(start - 1) == '#' && start > 0
|| toGo.charAt(start) == ' ' && toGo.charAt(start - 1) == ' ' && start > 0) {
toGo = toGo.substring(0, toGo.length() - 1);
} else if(toGo.charAt(start) == ' ') {
toGo += '#';
} else if(toGo.charAt(start) != '#' && toGo.charAt(start - 1) == ' ') {
toGo = toGo.substring(0, toGo.length() - 1) + '#' + toGo.charAt(start);
}
}
@Override
public void afterTextChanged(Editable s) {
if(lock) return;
toGo = toGo.replaceAll("#+", "#");
lock = true;
editHashtags.setText(toGo);
editHashtags.setSelection(toGo.length());
lock = false;
}
});
Проблема заключается в том, когда я использую lock
изменить текст. Похоже, что setText
действительно медленный, поэтому, если я набираю текст быстро, некоторые символы игнорируются.
Каков наилучший подход для решения этой проблемы?
спасибо.