I am a learning via a book so please forgive this newbie question.
I have a bunch of imageButtons in my xml, here is how one of them looks:
<ImageButton android:src="@drawable/level1" android:layout_width="wrap_content"
android:id="@+id/imageButton1" android:layout_height="wrap_content"
android:onClick="button_clicked1"></ImageButton>
and processing code:
public void button_clicked1(View v) {
text1.setText("clicked");
}
rather than have each button have its separate onClick code, is there anyway I can pass which button was clicked? for example button_clicked(1) and then button_clicked(2) instead of button_clicked1 like it is now (in the above example xml code)
or i have no choice but have to do it separately?
Kind of – what I like to do is make my View or Activity implement View.OnClickListener.
Then during onCreate, I do something like:
then, in my onclick:
Of course, you can definitely get creative and toss the switch statement if any of your buttons should trigger the same event behavior. Also, I’m not in a place where I can easily view my droid references so there may be an OnClickListener specific to ImageButton – if so, implement that on your containing View or Activity to consolidate the handlers…
Hope that makes sense – happy coding!
B