比较android中的drawables imageButtons

问题描述

我有 6 个带有可绘制对象的图像按钮,我正在尝试使用 getDrawable() 比较它们。 这是我的 xml 代码

'''

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"
tools:context=".TicTacToe">

<ImageButton
    android:id="@+id/imageButton1"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_marginStart="25dp"
    android:layout_marginTop="147dp"
    android:layout_marginEnd="5dp"
    android:layout_marginBottom="14dp"
    android:onClick="change"
    app:layout_constraintBottom_toTopOf="@+id/imageButton2"
    app:layout_constraintEnd_toStartOf="@+id/imageButton01"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:srcCompat="@drawable/blank" />

<ImageButton
    android:id="@+id/imageButton01"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_marginTop="147dp"
    android:layout_marginEnd="5dp"
    android:layout_marginBottom="14dp"
    android:onClick="change"
    app:layout_constraintBottom_toTopOf="@+id/imageButton02"
    app:layout_constraintEnd_toStartOf="@+id/imageButton001"
    app:layout_constraintStart_toEndOf="@+id/imageButton1"
    app:layout_constraintTop_toTopOf="parent"
    app:srcCompat="@drawable/blank" />

<ImageButton
    android:id="@+id/imageButton2"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_marginStart="24dp"
    android:layout_marginEnd="6dp"
    android:layout_marginBottom="10dp"
    android:onClick="change"
    app:layout_constraintBottom_toTopOf="@+id/imageButton3"
    app:layout_constraintEnd_toStartOf="@+id/imageButton02"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/imageButton1"
    app:srcCompat="@drawable/blank" />

<ImageButton
    android:id="@+id/imageButton001"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_marginTop="148dp"
    android:layout_marginEnd="16dp"
    android:layout_marginBottom="13dp"
    android:onClick="change"
    app:layout_constraintBottom_toTopOf="@+id/imageButton002"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toEndOf="@+id/imageButton01"
    app:layout_constraintTop_toTopOf="parent"
    app:srcCompat="@drawable/blank" />

<ImageButton
    android:id="@+id/imageButton02"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_marginEnd="7dp"
    android:layout_marginBottom="10dp"
    android:onClick="change"
    app:layout_constraintBottom_toTopOf="@+id/imageButton03"
    app:layout_constraintEnd_toStartOf="@+id/imageButton002"
    app:layout_constraintStart_toEndOf="@+id/imageButton2"
    app:layout_constraintTop_toBottomOf="@+id/imageButton01"
    app:srcCompat="@drawable/blank" />

<ImageButton
    android:id="@+id/imageButton002"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_marginEnd="14dp"
    android:layout_marginBottom="10dp"
    android:onClick="change"
    app:layout_constraintBottom_toTopOf="@+id/imageButton003"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toEndOf="@+id/imageButton02"
    app:layout_constraintTop_toBottomOf="@+id/imageButton001"
    app:srcCompat="@drawable/blank" />

<ImageButton
    android:id="@+id/imageButton3"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_marginStart="24dp"
    android:layout_marginEnd="6dp"
    android:layout_marginBottom="200dp"
    android:onClick="change"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toStartOf="@+id/imageButton03"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/imageButton2"
    app:srcCompat="@drawable/blank" />

<ImageButton
    android:id="@+id/imageButton03"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_marginEnd="7dp"
    android:layout_marginBottom="200dp"
    android:onClick="change"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toStartOf="@+id/imageButton003"
    app:layout_constraintStart_toEndOf="@+id/imageButton3"
    app:layout_constraintTop_toBottomOf="@+id/imageButton02"
    app:srcCompat="@drawable/blank" />

<ImageButton
    android:id="@+id/imageButton003"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_marginEnd="14dp"
    android:layout_marginBottom="200dp"
    android:onClick="change"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toEndOf="@+id/imageButton03"
    app:layout_constraintTop_toBottomOf="@+id/imageButton002"
    app:srcCompat="@drawable/blank" />

'''

kotlin 代码

'''

类井字游戏:AppCompatActivity() {

var spot1: ImageButton? =null
var spot01: ImageButton? =null
var spot001: ImageButton? =null
var spot2: ImageButton? =null
var spot02: ImageButton? =null
var spot002: ImageButton? =null
var spot3: ImageButton? =null
var spot03: ImageButton? =null
var spot003: ImageButton? =null

override fun onCreate(savedInstanceState: Bundle?)

{

    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_tic_tac_toe)
    spot1 = findViewById<ImageButton>(R.id.imageButton1)
    spot01 = findViewById<ImageButton>(R.id.imageButton01)
    spot001 = findViewById<ImageButton>(R.id.imageButton001)
    spot2 = findViewById<ImageButton>(R.id.imageButton2)
    spot02 = findViewById<ImageButton>(R.id.imageButton02)
    spot002 = findViewById<ImageButton>(R.id.imageButton002)
    spot3 = findViewById<ImageButton>(R.id.imageButton3)
    spot03 = findViewById<ImageButton>(R.id.imageButton03)

    spot003 = findViewById<ImageButton>(R.id.imageButton003)

}

val photos: IntArray= intArrayOf(R.drawable.x,R.drawable.o,R.drawable.blank)

var turn = 1
var winState = false

fun change(view: View){
    var spot = findViewById<ImageButton>(view.getId()) //as Button

    if(turn == 1) {
        spot.setimageResource(photos[0])
        turn = turn * -1
    }

    else{
        spot.setimageResource(photos[1])
        turn = turn * -1
    }

    if(spot1?.getDrawable() == spot01?.getDrawable() &&
        spot1?.getDrawable() == spot001?.getDrawable() &&
        spot1?.getDrawable() != photos[2].toDrawable())
    {
        winState = true;
        Toast.makeText(getApplicationContext(),"Winner",Toast.LENGTH_SHORT).show();
    }
}

}

'''

解决方法

Drawable 是一个对象,因此需要使用 equals() 方法进行比较。无论如何,我也不确定它是否会起作用,因为我不确定 Drawable 如何实现该方法,因此它可能不适合您的情况。

此外,您正在使用视图来检查业务逻辑的状态,这根本不理想。最好的解决方案是使用 Map 或矩阵(比方说,一个维度为 [3][3] 的数组),您可以在其中存储单个点的实际值,然后将可绘制对象与这些值进行协调在矩阵中。

我知道这会影响您代码中的所有内容,您必须对其进行大量更改,因此更快的解决方案可能是在 ImageButton 上使用 android:tag。您可以在其上指定任何对象,因此您也可以使用和 Integer 并对其进行比较,始终使用 equals() 方法。这意味着您可以依靠标签来处理您的逻辑。

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...