在EditText控件中更改文本0.5秒后,我该怎么办? [英] How can I do something 0.5 seconds after text changed in my EditText control?
本文介绍了在EditText控件中更改文本0.5秒后,我该怎么办?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用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秒后,我该怎么办?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文