亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Android TextWatcher內(nèi)容監(jiān)聽死循環(huán)案例詳解

 更新時(shí)間:2021年08月30日 09:34:00   作者:自己找知己  
這篇文章主要介紹了Android TextWatcher內(nèi)容監(jiān)聽死循環(huán)案例詳解,本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下

Android TextWatcher內(nèi)容監(jiān)聽死循環(huán)

TextWatcher如何避免在afterTextChanged中調(diào)用setText后導(dǎo)致死循環(huán),今天在用TextView時(shí),添加了addTextChangedListener方法監(jiān)聽內(nèi)容改變,在afterTextChanged方法中又執(zhí)行了setText方法,結(jié)果造成afterTextChanged方法再次調(diào)用,然后setText,因此造成了死循環(huán)的問(wèn)題。列出此問(wèn)題,以備后忘。

先貼Google文檔原文說(shuō)明:

/** * This method is called to notify you that, somewhere within * <code>s</code>, the text has been changed. * It is legitimate to make further changes to <code>s</code> from * this callback, but be careful not to get yourself into an infinite * loop, because any changes you make will cause this method to be * called again recursively. * (You are not told where the change took place because other * afterTextChanged() methods may already have made other changes * and invalidated the offsets. But if you need to know here, * you can use {@link Spannable#setSpan} in {@link #onTextChanged} * to mark your place and then look up from here where the span * ended up. */public void afterTextChanged(Editable s);

根據(jù)文檔說(shuō)明意思就是調(diào)用setText之前暫時(shí)去掉此監(jiān)聽器, 然后再恢復(fù)添加自身即可.

如下:

xxxEdit.addTextChangedListener(new TextWatcher() {
@Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
@Override public void onTextChanged(CharSequence s, int start, int before, int count) {}
@Override
public void afterTextChanged(Editable s) {
xxxEdit.removeTextChangedListener(this);
xxxEdit.setText("新取值");
xxxEdit.addTextChangedListener(this);
}
});

到此這篇關(guān)于Android TextWatcher內(nèi)容監(jiān)聽死循環(huán)案例詳解的文章就介紹到這了,更多相關(guān)Android TextWatcher內(nèi)容監(jiān)聽死循環(huán)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論