问题描述
我有重新present输入的搜索条件的EDITTEXT。我想知道如果有一种方式,当用户停止编辑这个EDITTEXT这样我就可以查询数据库为我的列表数据来检测。例如,如果用户键入测试我想要被通知后才用户键入的文本,而不是之后用户键入每个字母,如文本观察家一样。你有什么想法?我会避免使用一些定时器来衡量毫秒关键preS事件的经过。
I have an editText which represent an input for a search criteria. I want to know if there is a way to detect when user stops editing this editText so I can query the db for data for my list. For example, if the user types "test" I want to be notified only after user has typed the word, not after user types each letter, like text watcher does. Do you have any ideas? I would avoid to use some timer to measure milliseconds elapsed between key pres events.
推荐答案
不令人难以置信的优雅,但这应该工作。
Not incredibly elegant, but this should work.
初始化:
long idle_min = 4000; // 4 seconds after user stops typing
long last_text_edit = 0;
Handler h = new Handler();
boolean already_queried = false;
设置你可运行的,将被从文本观察家称为:
Set up your runnable that will be called from the text watcher:
private Runnable input_finish_checker = new Runnable() {
public void run() {
if (System.currentTimeMillis() > (last_text_edit + idle_min - 500)) {
// user hasn't changed the EditText for longer than
// the min delay (with half second buffer window)
if (!already_queried) { // don't do this stuff twice.
already_queried = true;
do_stuff(); // your queries
}
}
}
};
在文本观察家将这个:
last_text_edit = System.currentTimeMillis();
h.postDelayed(input_finish_checker, idle_min);
这篇关于安卓EDITTEXT:当检测到用户停止编辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!