-
Notifications
You must be signed in to change notification settings - Fork 0
/
DelayedTextWatcher.java
64 lines (49 loc) · 1.52 KB
/
DelayedTextWatcher.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package com.naukri.modules;
/**
* Created by sudeep.srivastava on 05-Aug-16.
*/
import android.os.AsyncTask;
import android.text.Editable;
import android.text.TextWatcher;
public abstract class DelayedTextWatcher implements TextWatcher {
private long delayTime;
private WaitTask lastWaitTask;
public DelayedTextWatcher(long delayTime) {
super();
this.delayTime = delayTime;
}
@Override
public void afterTextChanged(Editable s) {
synchronized (this) {
if (lastWaitTask != null){
lastWaitTask.cancel(true);
}
lastWaitTask = new WaitTask();
lastWaitTask.execute(s);
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// TODO Auto-generated method stub
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
}
public abstract void afterTextChangedDelayed(Editable s);
private class WaitTask extends AsyncTask<Editable, Void, Editable> {
@Override
protected Editable doInBackground(Editable... params) {
try {
Thread.sleep(delayTime);
} catch (InterruptedException e) {
}
return params[0];
}
@Override
protected void onPostExecute(Editable result) {
super.onPostExecute(result);
afterTextChangedDelayed(result);
}
}
}