Reputation: 5697
I have a simple android app that has quite a number of buttons, all doing ever-so-slightly different operations to a number. At present, I have a different method for each function. The method is called using the XML property "onClick" of the button.
I am trying to add even more buttons, but to save myself time and code, I thought I'd write one method for all buttons. This method should be passed a value telling it about the operation to be done.
I would really like to just be able to give the value that should be passed to that method in the XML, rather than having to write it as code using OnClickListener().
Is there a way to do this?
Upvotes: 2
Views: 970
Reputation: 13960
Actually, you are passing a value to the method: the id of the clicked button.
The click handler usually looks like:
public void ClickHandler(View v) {
switch(v.getId()) {
case BUTTON_X:
//do some stuff
case BUTTON_Y:
// do some other stuff
}
}
Upvotes: 6
Reputation: 29199
you can use tag or id of view object in method onClick(View view), and use these as you want to do.
Upvotes: 1