Android: Force Onkeylistener To Wait
I currently have some action associated with when the user types something in a AutocompleteTextView. However, I only want the action to register if the user stops typing. Essentia
Solution 1:
Create a Timer. When the Timer fires, do your action. If you receive another keypress before the Timer expires, cancel the timer, and reset it. Something like,
publicbooleanonKey(View v, int keyCode, KeyEvent event) {
if (mTimer != null) {
mTimer.cancel();
}
mTimer = newTimer();
mTimer.schedule(newTimerTask() {
@Overridepublicvoidrun() {
// do what you need to do after the one second here
mTimer = null;
}
}, 1000);
}
If you modify the view tree from inside the timer task's run()
method, you must force it to happen on the UI thread. You can do this like,
YouActivity.this.runOnUiThread(new Runnable() {
@Override
publicvoidrun() {
// modify your UI components here
}
});
Solution 2:
You could implement TimerTask to call my_function at a later time.
http://developer.android.com/reference/java/util/TimerTask.html
Solution 3:
I would try to ignore the key at first, send an delayed message (handler and co) which holds all key data, and deliver that key message later when the message was received.
However, I think you need other entry points the setOnKeyListener, may be onKeyEvent or dispatchKeyEvent
Just an idea
Post a Comment for "Android: Force Onkeylistener To Wait"