简体   繁体   中英

android imagebutton click event in xml

i define an imagebutton like this:

<ImageButton android:src="@raw/blaimage" /> 

now how can i also define which method should be called when the button is clicked.

in the android documentation it says that you can use onClick but it doesnt seem to compile for me.

 android:onClick="selfDestruct" 

and in the activity i have:

 public void selfDestruct(View view) {
     // Kabloey
 }

http://developer.android.com/reference/android/widget/Button.html

When you get inflated your layout your button is available by id. So you can set any code to be executed when the button is clicked:

Button button = (Button) findViewByID(R.id.button_id);
button.setOnClickListener(new OnClickListener() {
 void onClick(...) {
   // your code here
}
};

Remember, that you should specify the id of your button like this(here it is button_id )

just found out, what the problem was. i was targeting android 1.5 but this feature is only available since API level 4 which is 1.6

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM