我们实际上可以使用 text-watcher 覆盖函数来解决这个问题
以下是如何完成的示例
还附有完整描述的链接
private EditText searchText;
private TextView resultText;
private Timer timer;
private TextWatcher searchTextWatcher = new TextWatcher() {
@Override
public void afterTextChanged(Editable arg0) {
// user typed: start the timer
timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
// do required
}
}, 400); // 400ms delay to before executing run, if user stops typing after 400ms function will get fired
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// reset timer if user is typing
if (timer != null) {
timer.cancel();
}
}
};
来源:延迟编辑文本