android xml中的imagebutton点击事件

发布于 2024-10-03 04:29:17 字数 503 浏览 0 评论 0原文

我像这样定义一个图像按钮:

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

现在我如何定义单击按钮时应调用哪个方法。

在android文档中,它说你可以使用onClick,但它似乎不适合我编译。

 android:onClick="selfDestruct" 

在我的活动中:

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

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

峩卟喜欢 2024-10-10 04:29:17

刚刚发现,问题是什么。
我的目标是 android 1.5,但此功能仅在 API 级别 4(即 1.6)之后可用

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

离去的眼神 2024-10-10 04:29:17

当你的布局膨胀时,你的按钮可以通过 id 来使用。因此,您可以设置单击按钮时要执行的任何代码:

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

请记住,您应该像这样指定按钮的 id(这里是 button_id

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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文