开发者

android imagebutton click event in xml

开发者 https://www.devze.com 2023-01-25 04:26 出处:网络
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.

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 v开发者_如何学JAVAoid selfDestruct(View view) {
     // Kabloey
 }

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


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


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)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号