いくつかのImageViewがあり、すべて同じonClick()を使用しています。クリックした画像に応じて、特定のImageViewからIDまたはR.drawable intを取得して、別の画像を開くにはどうすればよいですか?
xmlレイアウトファイルにいくつかの画像ビューがあると思います。
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_content" >
<ImageView
android:id="@+id/image_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/image_drawable_1"
android:onClick="doSomething" />
<ImageView
android:id="@+id/image_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/image_drawable_2"
android:onClick="doSomething" />
<ImageView
android:id="@+id/image_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/image_drawable_3"
android:onClick="doSomething" />
</LinearLayout>
このメソッドをアクティビティに追加する必要があります。
public void doSomething(View v) {
switch (v.getId()) {
case R.id.image_1:
// You clicked on image 1
// If you need to do something to the ImageView with this particular Id then uncomment the next line.
// ImageView imageView = (ImageView) v;
break;
case R.id.image_2:
// You clicked on image 2
break;
case R.id.image_3:
// You clicked on image 3
break;
}
}
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加