1

我是android编程的新手。你如何使用 android:onClick?我将要调用的方法放在哪里?

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

<Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:text="Button"
        android:onClick="doSomething"/>
<ImageView
    android:id="@+id/icon"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_weight="1"
    android:adjustViewBounds="true"
    android:src="@drawable/molecule" />

我会将它放在需要进行此布局的 .java 文件中吗?

4

2 回答 2

6

是的,你需要把它放在你 setContentView 这个布局的活动类中。onclick 的方法应该是这样的形式:

public void doSomething(View v) {
}
于 2012-09-12T21:02:57.603 回答
2

我假设你想出了如何在你的活动中设置内容?在同一活动中,添加:

public void doSomething( View view ) {
    // onClick code goes here.
}
于 2012-09-12T21:03:11.153 回答