java - How to properly set custom long click listener? -
i'm trying set own long click listener on unlock button. whenever press unlock button summarize duration , can unlock permanently clicking.
unlock.setontouchlistener(new view.ontouchlistener() { @override public boolean ontouch(final view v, motionevent event) { if (event.getaction() == motionevent.action_down) { unlock.settext("press unlock"); islongpress = true; handler handler = new handler(); handler.postdelayed(new runnable() { @override public void run() { if (islongpress) { unlock(); } } }, longclickduration); //amount of time of long click } else if (event.getaction() == motionevent.action_up) { unlock.settext("unlock"); islongpress = false; } return true; } }); }catch (exception e) { // todo: handle exception } }
if want handle long clicks consider using following code:
unlock.setonlongclicklistener(new view.onlongclicklistener() { @override public boolean onlongclick(view v) { code } });
but if unlock();
should invoked after (customizable) amount of time, should measure time in motionevent.action_up
handler. @attaullah khan said, use systemclock.elapsedrealtime()
system timer correctly count number of milliseconds @ 2 moments (when button pressed , released) , if time greater longclickduration
invoke unlock
the handler.postdelayed
call in motionevent.action_down
handler invokes check of pressed state after longclickduration
interval , if button gets pressed @ moment, verification passes not correct
Comments
Post a Comment