我有一個editText表示搜索條件的輸入。我想知道是否有方法檢測用戶何時停止編輯此editText,以便我可以查詢數據庫以獲取我的列表的數據。例如,如果用戶鍵入「測試」,我只想在用戶鍵入單詞後纔會收到通知,而不是在用戶鍵入每個字母之後(例如文本觀察者所做的)。你有什麼想法?我會避免使用一些計時器來測量按鍵事件之間經過的毫秒數。android editText:當用戶停止編輯時檢測
回答
不令人難以置信的優雅,但這應該工作。
初始化:
long idle_min = 4000; // 4 seconds after user stops typing
long last_text_edit = 0;
Handler h = new Handler();
boolean already_queried = false;
設置你可運行將從文本觀察家稱爲:
last_text_edit = System.currentTimeMillis();
h.postDelayed(input_finish_checker, idle_min);
Thanx ..這真的對我有幫助.. – 2013-08-23 10:19:14
它只需要幾個字符,當我把它放在textwatcher就像當我鍵入「你好」然後它只需要「他」 – 2014-08-12 11:55:55
我得到了問題的解決方案。實際上每次它創建新的線程,所以我們需要停止早期的線程 因此,我們需要停止早期的線程 這裏是功能 h.removeCallbacks(input_finish_checker); – 2014-08-12 13:35:22
首先創建:
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
}
}
}
};
在文本觀察家將這個以下領域:
private Date _lastTypeTime = null;
然後確保你對你EDITTEXT工具 'TextWatcher':
_editText.addTextChangedListener(this);
然後,覆蓋interface`s方法如下:
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3)
{
_lastTypeTime = new Date();
}
@Override
public void afterTextChanged(Editable arg0)
{
}
@Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3)
{
// dispatch after done typing (1 sec after)
Timer t = new Timer();
TimerTask tt = new TimerTask()
{
@Override
public void run()
{
Date myRunTime = new Date();
if ((_lastTypeTime.getTime() + 1000) <= myRunTime.getTime())
{
post(new Runnable()
{
@Override
public void run()
{
Log.d("<tag>", "typing finished!!!");
}
});
}
else
{
Log.d("<tag>", "Canceled");
}
}
};
t.schedule(tt, 1000);
}
非常感謝@gor,這是迄今爲止最好的解決方案:D – 2013-07-11 18:50:24
很好的答案,但我認爲你需要該視圖才能讓發佈工作。 – Sotti 2014-09-29 14:17:05
方法'post'不存在 – 2015-08-19 16:38:59
這裏是你如何可以檢測事件中,你正在尋找對於。
聲明和初始化:
private Timer timer = new Timer();
private final long DELAY = 1000; // in ms
偵聽在例如onCreate()
EditText editText = (EditText) findViewById(R.id.editTextStopId);
editText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
@Override
public void onTextChanged(final CharSequence s, int start, int before,
int count) {
if(timer != null)
timer.cancel();
}
@Override
public void afterTextChanged(final Editable s) {
//avoid triggering event when text is too short
if (s.length() >= 3) {
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
queryDB();
}
}, DELAY);
}
}
});
所以,當文本被改變時,定時器開始等待下一次發生的改變。當它們發生時,定時器被取消,然後再次啓動。
這就是我爲我做的和爲我工作的!
long delay = 1000; // 1 seconds after user stops typing
long last_text_edit = 0;
Handler handler = new Handler();
private Runnable input_finish_checker = new Runnable() {
public void run() {
if (System.currentTimeMillis() > (last_text_edit + delay - 500)) {
// TODO: do what you need here
DoStaff();
}
}
};
EditText editText = (EditText) findViewById(R.id.editTextStopId);
editText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
@Override
public void onTextChanged(final CharSequence s, int start, int before,
int count) {
//You need to remove this to run only once
handler.removeCallbacks(input_finish_checker);
}
@Override
public void afterTextChanged(final Editable s) {
//avoid triggering event when text is empty
if (s.length() > 0) {
last_text_edit = System.currentTimeMillis();
handler.postDelayed(input_finish_checker, delay);
} else {
}
}
});
- 1. 如何檢測用戶是否停止在EditText中輸入android
- 2. 滾動EDITTEXT停止編輯它
- 3. 檢測用戶何時停止在EditText中寫入以保存內容
- 4. 當用戶停止觸摸UIButton時檢測到
- 5. 當用戶在jquery中啓動/停止輸入時檢測
- 6. 當圖庫停止滾動時檢測?
- 7. 當用戶在CKEditor中切換編輯模式時檢測到
- 8. 停止用戶輸入到編輯框?
- 9. Android:當水平滾動視圖停止滾動時檢測
- 10. 當第一次編輯後編輯列時如何停止時間戳更新
- 11. Android檢測用戶是否停止移動
- 12. Android EditText不可編輯
- 13. 當到達用戶名時停止mysql_fetch_array
- 14. jQuery的滾動()檢測時,用戶會停止滾動
- 15. Android:如何檢測用戶何時停止與麥克風講話
- 16. 如何檢測Android ListView滾動停止?
- 17. 檢測Android MediaPlayer是否停止播放?
- 18. 當禁用鍵盤時,EditText錯誤不會停止
- 19. 當用戶會話超時時檢測
- 20. 當用戶按主頁按鈕時停止Android服務
- 21. 當我編輯EditText中的數據時,BaseExpandableListAdapter調用getgroupview()
- 22. Android:OnlongClickListener,檢測它何時停止
- 23. jquery mousewheel:檢測輪子何時停止?
- 24. 當新的Android Intent開啓時,測距信號停止
- 25. 當用戶截圖時檢測到
- 26. Android:滾動視圖停止滾動時檢測
- 27. 如何編輯EditText
- 28. iOS的檢測將停止
- 29. 當另一個EditText清除焦點時停止EditText獲得焦點
- 30. 當用戶設置鬧鐘時檢測Android
難道你不能使用空格作爲分隔符嗎?在TextWatcher中指定正則表達式並驗證空格 – Neil 2013-03-19 10:24:42
不,也許用戶不按空格鍵,或者他只鍵入名稱的一半,他期望看到可用結果 – 2013-03-19 10:25:53
但是然後您想要使用字符。你想在用戶輸入時避免對你的數據庫有多個請求嗎? – Neil 2013-03-19 10:27:03