本文介紹了如何在 EditText 控件中的文本更改后 0.5 秒執行某些操作?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
限時送ChatGPT賬號..
我正在使用 EditText 控件過濾我的列表.我想在用戶完成 EditText 輸入后 0.5 秒過濾列表.為此,我使用了 TextWatcher
的 afterTextChanged
事件.但是這個事件會隨著 EditText 中每個字符的變化而上升.
I am filtering my list using an EditText control. I want to filter the list 0.5 seconds after the user has finished typing in EditText. I used the afterTextChanged
event of TextWatcher
for this purpose. But this event rises for each character changes in EditText.
我該怎么辦?
推薦答案
使用:
editText.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) { }
private Timer timer = new Timer();
private final long DELAY = 1000; // Milliseconds
@Override
public void afterTextChanged(final Editable s) {
timer.cancel();
timer = new Timer();
timer.schedule(
new TimerTask() {
@Override
public void run() {
// TODO: Do what you need here (refresh list).
// You will probably need to use
// runOnUiThread(Runnable action) for some
// specific actions (e.g., manipulating views).
}
},
DELAY
);
}
}
);
訣竅在于每次 EditText
中的文本發生更改時取消和重新安排 Timer
.
The trick is in canceling and rescheduling Timer
each time, when text in EditText
gets changed.
關于設置延遲多長時間,請參閱這篇文章.
For how long to set the delay, see this post.
這篇關于如何在 EditText 控件中的文本更改后 0.5 秒執行某些操作?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!