events - Android OnClickListener - identify a button -


hey. have activity:

public class mtest extends activity {   button b1;   button b2;   public void oncreate(bundle savedinstancestate) {     ...     b1 = (button) findviewbyid(r.id.b1);     b2 = (button) findviewbyid(r.id.b2);     b1.setonclicklistener(myhandler);     b2.setonclicklistener(myhandler);     ...   }   view.onclicklistener myhandler = new view.onclicklistener() {     public void onclick(view v) {       // question starts here!!!       // if b1       // if b2       // question ends here!!!     }   } } 

how check button has been clicked?

you learn way it, in easy way, is:

public class mtest extends activity {   button b1;   button b2;   public void oncreate(bundle savedinstancestate) {     ...     b1 = (button) findviewbyid(r.id.b1);     b2 = (button) findviewbyid(r.id.b2);     b1.setonclicklistener(myhandler1);     b2.setonclicklistener(myhandler2);     ...   }   view.onclicklistener myhandler1 = new view.onclicklistener() {     public void onclick(view v) {       // 1st button     }   };   view.onclicklistener myhandler2 = new view.onclicklistener() {     public void onclick(view v) {       // 2nd button     }   }; } 

or, if working 1 clicklistener, can do:

view.onclicklistener myonlyhandler = new view.onclicklistener() {   public void onclick(view v) {       switch(v.getid()) {         case r.id.b1:           // first button           break;         case r.id.b2:           // second button           break;       }   } } 

though, don't recommend doing way since have add if each button use. that's hard maintain.


Comments

Popular posts from this blog

c++ - How do I get a multi line tooltip in MFC -

asp.net - In javascript how to find the height and width -

c# - DataTable to EnumerableRowCollection -