开发者

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 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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜