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 秒执行某些操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在 EditText 控件中的文本更改后 0.5 秒执行某


- android 4中的android RadioButton问题 2022-01-01
- 想使用ViewPager,无法识别android.support.*? 2022-01-01
- 用 Swift 实现 UITextFieldDelegate 2022-01-01
- MalformedJsonException:在第1行第1列路径中使用JsonReader.setLenient(True)接受格式错误的JSON 2022-01-01
- Android - 我如何找出用户有多少未读电子邮件? 2022-01-01
- Android viewpager检测滑动超出范围 2022-01-01
- 使用自定义动画时在 iOS9 上忽略 edgesForExtendedLayout 2022-01-01
- Android - 拆分 Drawable 2022-01-01
- 在测试浓缩咖啡时,Android设备不会在屏幕上启动活动 2022-01-01
- 如何检查发送到 Android 应用程序的 Firebase 消息的传递状态? 2022-01-01