单击时显示新图像

Show new image onClick

我正在制作问答游戏。如果按下正确的按钮(4 个选项),我想显示另一张图片。上图是问号,下图是人。

我有这两个ImageView

    <ImageView
    android:layout_width="250dp"
    android:layout_height="250dp"
    android:id="@+id/imageView"
    android:src="@drawable/et"
    android:layout_below="@+id/textView"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="19dp" />

<ImageView
    android:layout_width="250dp"
    android:layout_height="250dp"
    android:id="@+id/imageView2"
    android:src="@drawable/questionmark"
    android:layout_alignTop="@+id/imageView"
    android:layout_alignStart="@+id/imageView"
    android:layout_alignParentStart="false"
    android:layout_alignParentEnd="false" />

都在我的 RelativeLayout 中。 ImageView2 与 ImageView 重叠。 在图片之间切换或删除 ImageView2 的最佳方法是什么?

如果按下了private button knapEt,ImageView2应该被移除或者分层切换,所以显示ImageView。

使用简单的逻辑,首先只显示一个视图,另一个不可见使用 android:visibility="gone" 属性。

<ImageView
android:layout_width="250dp"
android:layout_height="250dp"
android:id="@+id/imageView"
android:src="@drawable/et"
android:layout_below="@+id/textView"
android:layout_centerHorizontal="true"
android:layout_marginTop="19dp" 
android:visibility="gone"/>

<ImageView
android:layout_width="250dp"
android:layout_height="250dp"
android:id="@+id/imageView2"
android:src="@drawable/questionmark"
android:layout_alignTop="@+id/imageView"
android:layout_alignStart="@+id/imageView"
android:layout_alignParentStart="false"
android:layout_alignParentEnd="false" />

然后使用代码切换图像。每次单击时,第一个变为不可见,第二个变为可见,反之亦然。

boolean flag = true;

public void toggleImage()
    {

            if (flag == true)
            {

                image.setVisibility(View.GONE);
                imageView2.setVisibility(View.VISIBLE);
             }
            else
            {
                //make the background visible
                image.setVisibility(View.VISIBLE);
                imageView2.setVisibility(View.GONE);
            }


    }