android - Single method to implement onTouchListener() for multiple buttons -
i trying see if there way create single method implement touch listener multiple buttons, seeing have quite few buttons same thing. difference in message send through sendmessage() method, , how long button needs held down message sent. if there way it, might be? , also, why wouldn't work?
//within oncreate method... button mbutton = (button) findviewbyid(r.id.three_sec_button); mbutton = addtouchtimer(mbutton, 3, 3);
calls -
private button addtouchtimer(button button, final int sec, final int messagenum){ button.setontouchlistener(new view.ontouchlistener() { boolean longenough = false; long realtimeleft = sec * 1000; @override // make message sent if button held down 3 seconds // otherwise won't send. sent during hold down process, releasing returns false // value , no message sent. public boolean ontouch(view arg0, motionevent arg1) { log.d("button", "touchy touchy!"); if(arg1.getaction() == motionevent.action_down){ buttonpresstime = new countdowntimer(realtimeleft, 1000){ @override public void ontick(long millisuntildone){ realtimeleft = millisuntildone; } @override public void onfinish() { long timeleft = realtimeleft; long currtime = system.currenttimemillis(); long realfinishtime = currtime + timeleft; while(currtime < realfinishtime){ currtime = system.currenttimemillis(); } longenough = true; sendemergencymessage(longenough, messagenum); } }.start(); } else if(arg1.getaction() == motionevent.action_up){ buttonpresstime.cancel(); sendmessage(longenough, messagenum); } return longenough; } }); return button; }
it seems efficiency's sake there has better way of doing implementing individual listeners each button. note, sendmessage() has log call in utilizes boolean value, want see set when passed. reason called during release of button.
yes right, there better way. single touchlistener handles everything, determining button via id.
void intialization(){ button m1, m2, m3, m4; ... //do initialization stuff m1.setid(1); m2.setid(2); m3.setid(3); m4.setid(4); mytouchlistener touchlistener = new mytouchlistener(); m1.setontouchlistener(touchlistener); m2.setontouchlistener(touchlistener); m3.setontouchlistener(touchlistener); m4.setontouchlistener(touchlistener); } public class mytouchlistener implements ontouchlistener { @override public boolean ontouch(view v, motionevent event) { switch(v.getid()){ case 1: //do stuff button 1 break; case 2: //do stuff button 2 break; case 3: //do stuff button 3 break; case 4: //do stuff button 4 break; } return true; } }
and that's how you'd it! numerical approach id's helpful in case. approach have activity implement ontouchlistener in activity, , code simpler.
public class myactivity extends activity implements ontouchlistener { void initialization(){ button m1, m2, m3, m4; ... //do initialization stuff m1.setid(1); m2.setid(2); m3.setid(3); m4.setid(4); m1.setontouchlistener(this); m2.setontouchlistener(this); m3.setontouchlistener(this); m4.setontouchlistener(this); } @override public boolean ontouch(view v, motionevent event) { switch(v.getid()){ case 1: //do stuff button 1 break; case 2: //do stuff button 2 break; case 3: //do stuff button 3 break; case 4: //do stuff button 4 break; } return true; } }
note: approach also work onclicklistener, oncheckedchangelistener, or other listener set on android view.
Comments
Post a Comment