實際上,我們可以使用文字觀察家覆蓋功能來解決這個問題
下面是一個例子,以它是如何做到
與完整的描述還附上鍊接
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();
}
}
};
來源:delayed edit text
我是新來這一點,但我不認爲onFocusChangeListener就在這裏工作。它是一個顯示項目列表的單個搜索欄。輸入後,列表將被縮小爲只包含以輸入的字符開頭的字符串。我希望在用戶停止鍵入1/2秒後更新頁面。 –
你說得對onFocusChangeListener你的情況,但你可以更新每個偶數或奇數字符的結果..我不建議你使用延遲,因爲我不認爲這是一個很好的解決方案。 – Cata
我有同樣的問題,這個代碼總是會調用運行(),當你輸入速度快,不是好主意 – vuhung3990