TextView textView=new TextView(context);
textView.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) {
s.append("A");
}
});
如果我们向TextWatcher
添加TextView
,并且我想在每次用户在其中写一封信时附加一个字母TextView
,但这样可以保留 - 调用TextWatcher
听众,依此类推StackOverFlow error
,那么如何在不重新调用TextWatcher
听众的情况下附加文字?
答案 0 :(得分:30)
很简单:
@Override
public void afterTextChanged(Editable s) {
editText.removeTextChangedListener(this);
//Any modifications at this point will not be detected by TextWatcher,
//so no more StackOverflowError
s.append("A");
editText.addTextChangedListener(this);
}
答案 1 :(得分:6)
避免stackoverflow的另一种方法:
TextView textView=new TextView(context);
textView.addTextChangedListener(new TextWatcher() {
boolean editing=false;
@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) {
if (!editing){
editing=true;
s.append("A");
editing=false;
}
}
});
答案 2 :(得分:4)
afterTextChanged的文档说:
调用此方法通知您,在s中的某个位置,文本已更改。从这个回调中对s进行进一步更改是合法的,但要注意不要陷入无限循环,因为你所做的任何更改都会导致这个方法再次被递归调用。 (您不会被告知发生了更改的位置,因为其他afterTextChanged()方法可能已经进行了其他更改并使偏移无效。但如果您需要知道此处,可以使用setSpan(Object, int, int, int)
中的onTextChanged(CharSequence, int, int, int)
来标记你的位置,然后从这里查看跨度最终的位置。
所以,每s.append("A")
你call afterTextChanged()
再次等等。
答案 3 :(得分:0)
一些伪代码,所以你可以这样做:
只需改变焦点....
像这样
tv.isFocusable = false
tv.setText("my new text")
tv.isFocusable = true //maybe post this to messsage queue so other jobs finish fist.
// later on in your listener:
if(tv.isFocusable && tv.hasFocus())
// do something
else ignore
答案 4 :(得分:-2)
Kotlin版
editText.addTextChangedListener(object: TextWatcher {
override fun afterTextChanged(s: Editable?) {
if (s.toString().isNotBlank()) {
val formattedValue: String = // Do some formatting
editText.removeTextChangedListener(this)
editText.setText(formattedValue)
editText.setSelection(editText.text.toString().length)
editText.addTextChangedListener(this)
}
}
override fun beforeTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) { }
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
}
})