Moh*_*san 9 android textview textwatcher
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");
}
});
Run Code Online (Sandbox Code Playgroud)
如果我们添加一个TextWatchera TextView,并且我想在其中附加一个字母TextView,每次用户在其中写一个字母,但这会不断重新调用TextWatcherListener,依此类推StackOverFlow error,所以如何附加一个文本而不重新打电话给TextWatcher听众?
And*_*nko 31
这很简单:
@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);
}
Run Code Online (Sandbox Code Playgroud)
小智 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;
}
}
});
Run Code Online (Sandbox Code Playgroud)
afterTextChanged的文档说:
调用此方法以通知您s内某处的文本已更改。从此回调中进一步对s进行更改是合理的,但请注意不要陷入无限循环,因为您进行的任何更改都将导致递归再次调用此方法。(你是不是说当变化发生,因为其他afterTextChanged()方法可能已经做了修改和无效的偏移量。但是,如果你需要知道这里,你可以使用setSpan(Object, int, int, int)在onTextChanged(CharSequence, int, int, int)标记你的地方,然后从这里那里仰望跨度结束了。
因此,再次与s.append("A")大家call afterTextChanged()见面等等。
| 归档时间: |
|
| 查看次数: |
11004 次 |
| 最近记录: |