java - How to set 4 button on one Listener? -
this simple method don't know code mean this
button1 = (button)findviewbyid(r.id.b1); button2 = (button)findviewbyid(r.id.b2); button3 = (button)findviewbyid(r.id.b3); button4 = (button)findviewbyid(r.id.b4); //button1-button4.setonclicklistener(new view.onclicklistener() { public void onclick(view button1-4){ { //do same method button1-4 } } }); button5 = (button)findviewbyid(r.id.b5); button6 = (button)findviewbyid(r.id.b6); button7 = (button)findviewbyid(r.id.b7); button8 = (button)findviewbyid(r.id.b8); //button5-button8.setonclicklistener(new view.onclicklistener() { public void onclick(view button5-8){ { //do same method button5-8 } } }); button9 = (button)findviewbyid(r.id.b9); button10 = (button)findviewbyid(r.id.b10); button11 = (button)findviewbyid(r.id.b11); button12 = (button)findviewbyid(r.id.b12); //button5-button8.setonclicklistener(new view.onclicklistener() { public void onclick(view button9-12){ { //do same method button9-12 } } });
so have 12 button on project put in 3 group(layout), each group have 4 button inside mean 4 button in each group same thing.
can me give sample code? thank's
this 1 template. hope help.
public class myactivity extends activity implements view.onclicklistener { button button1, button2, button3; @override public void oncreate(bundle bundle) { super.oncreate(); ... button1 = (button) findviewbyid(r.id.button1); button2 = (button) findviewbyid(r.id.button2); button3 = (button) findviewbyid(r.id.button3); button1.setonclicklistener(this); button2.setonclicklistener(this); button3.setonclicklistener(this); } @override public void onclick(view v) { switch(v.getid()) { case r.id.button1: // stuff; can called method here also. break; case r.id.button2: // stuff; break; ... } }
you can set in layout xml using android:onclick attribute.
android:onclick="onclick"
then in activity class add onclick method.
public void onclick(view v) { //write code here if(v.getid==r.id.button1 || v.getid==r.id.button2 || v.getid==r.id.button3 ||v.getid==r.id.button4){ // stuff } }
Comments
Post a Comment