I'm developing a sample app for android and my level is beginner.
For simplicity assume that I have three buttons namely A,B,C.
Instead of these buttons triggering different functions every time as they are clicked, all these buttons trigger the same method "onClick" which is defined in XML.
How can I开发者_如何学JAVA get the name of the button that triggered onClick method, in what way should I use the view class ?
Thanks in advance for your time.
Take a look at this. Should do exactly what you want, with some suggestions for other implementations as well :)
Another Way.
public void onClick(View v) {
// Perform action on click
String msg;
int id = v.getId();
Button inB = (Button) v.findViewById(id);
msg = "Clicked Button ID From Object " + inB.getId() + " Button";
Toast.makeText(v.getContext(), msg, Toast.LENGTH_LONG).show();
}
精彩评论