1
当用户停止输入后,我必须运行一次函数,但我的函数运行多次。我检测到用户停止时打字,但不能只执行一次我的功能。当用户停止输入但我的函数运行多次时,我必须运行一次函数
@Override
public void onTextChanged(CharSequence charSequence, final int i, final
int i1, final int i2) {
Timer timer = new Timer();
//Run after user stops typing
TimerTask tt = new TimerTask() {
@Override
public void run() {
Date myRunTime = new Date();
if ((lastTypeTime.getTime() + 1000) <= myRunTime.getTime()) {
Handler h = new Handler(Looper.getMainLooper());
h.post(new Runnable() {
@Override
public void run() {
Log.i("t_type", "Type Finish");
//Issue : This is printed multiple time.
}
});
} else {
Log.i("t_cancel", "cancel");//cancel
}
}
};
timer.schedule(tt, 2000);//two second delay after user stops typing
}
你怎么知道用户什么时候停止输入才能显示你的代码。 –
这是因为你用timerTask尝试了不同的AlarmManager。使用http://www.vogella.com/tutorials/AndroidTaskScheduling/article.html –
它每2秒运行 –