1、
在xml檔案中配置android:onClick=“方法名”
2、
在所在activity中定義onClick方法
看下具體的實作:
1.main.xml檔案
<?xml version="1.0" encoding="utf-8"?>
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/bg"
android:scrollbars="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<!-- 橫排開始 -->
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginTop="15dp"
android:background="@drawable/circle_corner1"
android:orientation="vertical" >
<ImageView
android:id="@+id/mg1"
android:onClick="myClick"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_gravity="left"
android:padding="10dp"
android:scaleType="fitXY"
android:src="@drawable/img01" />
</LinearLayout>
android:onClick="myClick"
</LinearLayout>
<!-- 橫排結束 -->
<!-- 橫排開始 -->
<!-- 橫排開始 -->
</LinearLayout>
</ScrollView>
2. MyOnClickListener.java檔案
public void myClick(View target) {
Intent intent;
switch (target.getId()) {
case R.id.mg1:
intent = new Intent(ImageListActivity.this, ImageSwitcher.class);
startActivity(intent);
break;
case R.id.mg2:
intent = new Intent(ImageListActivity.this, ImageSwitcher.class);
case R.id.mg3:
case R.id.mg4:
.......
default:
}
}