我正在制作一个TicTacToe,其中有一个网格背景,它包含九个ImageViews'。
底部有一个重置按钮,点击它所有的ImageViews'应该消失(将它们设置为白色)。
这是应用程序启动时的初始状态,再次单击重置按钮后会显示
以下是MainActivity.java: - 在btnTap函数中写什么,设置为重置按钮的OnClick,以便实现我的目的?
(除了getTag()之外的任何其他逻辑将不胜感激)
package com.example.honey1.tictactoe;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity
{
int active = 0;
int[] state = {2, 2, 2, 2, 2, 2, 2, 2, 2};
public void btnTap(View view)
{
for (int i = 0; i < 9; i++)
{
if(state[i]!=2)
{
//What to write here?
}
}
}
public void imgTap(View view) {
ImageView img = (ImageView) view;
int tapped = Integer.parseInt(img.getTag().toString());
if (state[tapped] == 2) {
if (active == 0) {
img.setImageResource(R.drawable.cross);
img.animate().setDuration(1000).rotation(90);
active = 1;
state[tapped]=0;
} else {
img.setImageResource(R.drawable.circle);
active = 0;;
state[tapped]=1;
}
} else {
Toast.makeText(getApplicationContext(), "Invalid Click!", Toast.LENGTH_SHORT).show();
}
}
@Override
protected void onCreate (Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
XML文件:(此应用仅包含一项活动)
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background"
tools:context="com.example.honey1.tictactoe.MainActivity">
<LinearLayout
android:id="@+id/linearLayout2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
android:orientation="horizontal"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.911"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp">
<Button
android:id="@+id/button"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_weight="1"
android:background="@drawable/button"
android:onClick="btnTap"
android:text="@string/btn"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.553"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.941"
tools:text="@string/btn" />
</LinearLayout>
<ImageView
android:id="@+id/h"
android:tag="0"
android:layout_width="69dp"
android:layout_height="57dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:onClick="imgTap"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.086"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.223"
app:srcCompat="@android:color/background_light" />
<ImageView
android:id="@+id/f"
android:tag="1"
android:layout_width="69dp"
android:layout_height="57dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:onClick="imgTap"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.501"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.223"
app:srcCompat="@android:color/background_light" />
<ImageView
android:id="@+id/b"
android:tag="2"
android:layout_width="69dp"
android:layout_height="57dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:onClick="imgTap"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.899"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.223"
app:srcCompat="@android:color/background_light" />
<ImageView
android:id="@+id/i"
android:tag="3"
android:layout_width="69dp"
android:layout_height="57dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="3dp"
android:layout_marginRight="8dp"
android:layout_marginStart="3dp"
android:layout_marginTop="8dp"
android:onClick="imgTap"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.101"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.436"
app:srcCompat="@android:color/background_light" />
<ImageView
android:id="@+id/g"
android:tag="4"
android:layout_width="69dp"
android:layout_height="57dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:onClick="imgTap"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.501"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.436"
app:srcCompat="@android:color/background_light" />
<ImageView
android:id="@+id/a"
android:tag="5"
android:layout_width="69dp"
android:layout_height="57dp"
android:layout_marginBottom="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
android:onClick="imgTap"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.899"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.436"
app:srcCompat="@android:color/background_light"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp" />
<ImageView
android:id="@+id/d"
android:tag="6"
android:layout_width="69dp"
android:layout_height="57dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:onClick="imgTap"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.086"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.655"
app:srcCompat="@android:color/background_light" />
<ImageView
android:id="@+id/c"
android:tag="7"
android:layout_width="69dp"
android:layout_height="57dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:onClick="imgTap"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.501"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.655"
app:srcCompat="@android:color/background_light" />
<ImageView
android:id="@+id/e"
android:tag="8"
android:layout_width="69dp"
android:layout_height="57dp"
android:layout_marginBottom="8dp"
android:layout_marginLeft="8dp"
android:onClick="imgTap"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.876"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.661"
app:srcCompat="@android:color/background_light"
android:layout_marginStart="8dp" />
答案 0 :(得分:0)
您无需识别ImageView,甚至无需检查哪些更改,只需迭代它们并根据需要进行设置。并且不要忘记将所有状态设置为2
for(int index=0; index<((ViewGroup)viewGroup).getChildCount(); ++index) {
View nextChild = ((ViewGroup)viewGroup).getChildAt(index);
if(nextChild instanceof ImageView){
//remove image
}
}