【发布时间】:2021-08-02 06:11:05
【问题描述】:
我有 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();
}
}
}
'''
【问题讨论】:
-
我不认为你可以像这样比较
Drawables,它们会返回不同的Drawable对象,因此比较总是错误的。我要做的是使用ImageButton和Drawable字段创建一个类Spot,并使用相同的Drawable对象来初始化Spots,其Drawable是相同的。然后比较这些字段;因为它们是用相同的对象初始化的,所以比较应该返回 true。
标签: android image drawable android-imagebutton