Tuesday 15 April 2014

eclipse - Android Button Push (Updated) -



eclipse - Android Button Push (Updated) -

i building app panic alert button. want display toast message after hold button 3 seconds if person hits error wont send not sure how it. help appreciated.

public long starttime = 0;

button btn = (button)findviewbyid(r.id.btnalert);

btn.setontouchlistener(new view.ontouchlistener(){ @override public boolean ontouch(view v, motionevent event){ if(event.getaction() == motionevent.action_up){ if((system.currenttimemillis()- starttime)> 3000){ toast.maketext(getapplicationcontext(), "alert received! emergency services arrive @ location shortly", toast.length_long).show(); } homecoming true; } if(event.getaction() == motionevent.action_down){ starttime = system.currenttimemillis(); homecoming true; } homecoming false; } });

do ontouchevent. each view can add together ontouchlistener(). when receives action_down event read actual time , store in starttime variable. if receives action_up event subtract actual time starttime. if time between starttime , actual time greater 3000 milliseconds show toast message. can't test next code right should work.

public long starttime = 0; button.setontouchlistener(new ontouchlistener() { @override public boolean ontouch(view v, motionevent event) { if(event.getaction() == motionevent.action_up){ if((system.currenttimemillis() - starttime) > 3000) { //show toast message } homecoming true; } if(event.getaction() == motionevent.action_down){ starttime = system.currenttimemillis(); homecoming true; } homecoming false; } });

android eclipse button time onclick

No comments:

Post a Comment