即使 isClickable 属性 设置为 false,我也可以点击按钮

I can click buttons even when the isClickable property set to false

Activity代码

在这里,enableOrDisable(),我正在调用 if myValue = 2,并且我已经按照之前的意图发送了 2 值 activity 但是,我仍然可以点击按钮,请发送一些解决方案该怎么做或我在做什么错?帮助将不胜感激。我正在尝试这样做,如果 myValue 不是 2 那么我的按钮都可以点击但是一旦 myValue 1 的玩家点击它就会在 firebase 中注册并被两个玩家收听并且在听完点击更改的玩家之后所有剩余的未点击按钮都为 false 和听的玩家,那个播放器按钮变为可点击但我不知道为什么两个玩家可以同时点击。

class OnlineGame : AppCompatActivity() {
private var player1Record = 0
private var player2Record = 0
private var myValue:Int ?= null
private lateinit var scoreCard: TextView
private var myClicks = ArrayList<Int>()
private var choices = arrayListOf<Int>(1,2,3,4,5,6,7,8,9)
private var count = 0
private val database = FirebaseDatabase.getInstance("https://tictactoe8088-default-rtdb.asia-southeast1.firebasedatabase.app")
private val myRef = database.reference
private var sessionId:String ?= null
override fun onCreate(savedInstanceState: Bundle?) {
    sessionId = intent.getStringExtra("sessionId").toString()
    myValue = intent.getIntExtra("playerValue",10)
    if(myValue == 2)
        enableOrDisable(false)
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_online_game)
    listenToChangeColors()
}

private fun enableOrDisable(trueOrfalse: Boolean){
    Log.d(TAG, "enableOrDisable: myValue = 2")
    for(i in choices){
        when(i){
            1 -> findViewById<Button>(R.id.button1)?.isEnabled = trueOrfalse
            2 -> findViewById<Button>(R.id.button2)?.isEnabled = trueOrfalse
            3 -> findViewById<Button>(R.id.button3)?.isEnabled = trueOrfalse
            4 -> findViewById<Button>(R.id.button4)?.isEnabled = trueOrfalse
            5 -> findViewById<Button>(R.id.button5)?.isEnabled = trueOrfalse
            6 -> findViewById<Button>(R.id.button6)?.isEnabled = trueOrfalse
            7 -> findViewById<Button>(R.id.button7)?.isEnabled = trueOrfalse
            8 -> findViewById<Button>(R.id.button8)?.isEnabled = trueOrfalse
            9 -> findViewById<Button>(R.id.button9)?.isEnabled = trueOrfalse
        }
    }
}

private fun listenToChangeColors() {
    var whoClicked:Int ?=  null
    myRef.child("Users").child("GameSessions").child(sessionId.toString()).child("ButtonClicked")
        .addValueEventListener(object:ValueEventListener{
            override fun onDataChange(snapshot: DataSnapshot) {
                val td = snapshot.value.toString().toInt()  /* td = buttonClicked*/
                if(td.equals(1..9)){
                    Log.d(TAG, "onDataChange: condition is true")
                    when(td){
                        1 -> findViewById<Button>(R.id.button1)?.isClickable = false
                        2 -> findViewById<Button>(R.id.button2)?.isClickable = false
                        3 -> findViewById<Button>(R.id.button3)?.isClickable = false
                        4 -> findViewById<Button>(R.id.button4)?.isClickable = false
                        5 -> findViewById<Button>(R.id.button5)?.isClickable = false
                        6 -> findViewById<Button>(R.id.button6)?.isClickable = false
                        7 -> findViewById<Button>(R.id.button7)?.isClickable = false
                        8 -> findViewById<Button>(R.id.button8)?.isClickable = false
                        9 -> findViewById<Button>(R.id.button9)?.isClickable = false
                    }
                    choices.remove(td)
                    drawOnButton(td)
                }
            }
            private fun drawOnButton(buttonValue: Int) {
                myRef.child("Users").child("GameSessions").child(sessionId.toString()).child("whichPlayer")
                    .addListenerForSingleValueEvent(object:ValueEventListener{
                        override fun onDataChange(snapshot: DataSnapshot) {
                            whoClicked = snapshot.value.toString().toInt()
                        }
                        override fun onCancelled(error: DatabaseError) {
                            TODO("Not yet implemented")
                        }
                    })
                when(buttonValue){
                    1 -> draw(findViewById(R.id.button1),whoClicked)
                    2 -> draw(findViewById(R.id.button2),whoClicked)
                    3 -> draw(findViewById(R.id.button3),whoClicked)
                    4 -> draw(findViewById(R.id.button4),whoClicked)
                    5 -> draw(findViewById(R.id.button5),whoClicked)
                    6 -> draw(findViewById(R.id.button6),whoClicked)
                    7 -> draw(findViewById(R.id.button7),whoClicked)
                    8 -> draw(findViewById(R.id.button8),whoClicked)
                    9 -> draw(findViewById(R.id.button9),whoClicked)
                }
                if(whoClicked != myValue)
                    enableOrDisable(true)
                else
                    enableOrDisable(false)
                if(myValue == 1)
                {
                    myRef.child("Users").child("GameSessions").child(sessionId.toString()).child("whichPlayer").setValue(2)
                }
                else{
                    myRef.child("Users").child("GameSessions").child(sessionId.toString()).child("whichPlayer").setValue(1)
                }
            }

            private fun draw(buttonToDraw: Button?, whoClicked: Int?) {
                if(whoClicked == 1){
                    buttonToDraw?.setBackgroundColor(resources.getColor(R.color.player1Color))
                    buttonToDraw?.text = "X"
                }
                else{
                    buttonToDraw?.setBackgroundColor(resources.getColor(R.color.player2Color))
                    buttonToDraw?.text = "O"
                }
                return
            }

            override fun onCancelled(error: DatabaseError) {
                TODO("Not yet implemented")
            }
        })
}

fun buttonClicked(view: View) {
    val whichButton = view as Button
    var buttonId = 0
    when (whichButton.id) {
        R.id.button1 -> buttonId = 1
        R.id.button2 -> buttonId = 2
        R.id.button3 -> buttonId = 3
        R.id.button4 -> buttonId = 4
        R.id.button5 -> buttonId = 5
        R.id.button6 -> buttonId = 6
        R.id.button7 -> buttonId = 7
        R.id.button8 -> buttonId = 8
        R.id.button9 -> buttonId = 9
    }
    myRef.child("Users").child("GameSessions").child(sessionId.toString()).child("ButtonClicked").setValue(buttonId)
    myRef.child("Users").child("GameSessions").child(sessionId.toString()).child("whichPlayer").setValue(myValue)
    myClicks.add(buttonId)
    checkWin()
    count += 1
}

private fun checkWin() {
    val result = calculateWin(myClicks)
    if ( result == 1) {
        Toast.makeText(this, "Player 1 Won", Toast.LENGTH_SHORT).show()
        player1Record += 1
        restart()
    }

}
private fun calculateWin( clicks : ArrayList<Int>) : Int {
    if (clicks.contains(1) && clicks.contains(2) && clicks.contains(3))
        return 1
    else if (clicks.contains(4) && clicks.contains(5) && clicks.contains(6))
        return 1
    else if (clicks.contains(7) && clicks.contains(8) && clicks.contains(9))
        return 1
    else if (clicks.contains(1) && clicks.contains(4) && clicks.contains(7))
        return 1
    else if (clicks.contains(2) && clicks.contains(5) && clicks.contains(8))
        return 1
    else if (clicks.contains(3) && clicks.contains(6) && clicks.contains(9))
        return 1
    else if (clicks.contains(1) && clicks.contains(5) && clicks.contains(9))
        return 1
    else if (clicks.contains(3) && clicks.contains(5) && clicks.contains(7))
        return 1
    else if (count == 9) {
        Toast.makeText(this, "Draw", Toast.LENGTH_SHORT).show()
        restart()
        return 2
    }
    return 0
}

private fun restart() {
    TODO("Not Properly Implemented yet")
    var clearAllButton: Button?
    myClicks.clear()
    myClicks.clear()
    for (i in 1..9) {
        clearAllButton = when (i) {
            1 -> findViewById(R.id.button1)
            2 -> findViewById(R.id.button2)
            3 -> findViewById(R.id.button3)
            4 -> findViewById(R.id.button4)
            5 -> findViewById(R.id.button5)
            6 -> findViewById(R.id.button6)
            7 -> findViewById(R.id.button7)
            8 -> findViewById(R.id.button8)
            9 -> findViewById(R.id.button9)
            else -> null
        }
        clearAllButton!!.setBackgroundColor(resources.getColor(R.color.teal_200,null))
        clearAllButton.text = ""
        clearAllButton.isClickable = true
        count = 0
    }
    scoreCard = findViewById(R.id.win_records)
    scoreCard.text = "Player 1 : $player1Record\nPlayer 2 : $player2Record"
}

}

布局代码

<?xml version="1.0" encoding="utf-8"?>
<TableLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
android:id= "@+id/table_layout"
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:gravity="center"
tools:context=".OnlineGame">

    <TableRow
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#FFFFFF"
        android:gravity="center">

        <Button
            android:id="@+id/button1"
            android:layout_width="40pt"
            android:layout_height="40pt"
            android:layout_marginStart="5pt"
            android:onClick="buttonClicked"
            app:backgroundTint="@color/teal_200"
            style="?android:attr/buttonBarButtonStyle" />

        <Button
            android:id="@+id/button2"
            android:layout_width="40pt"
            android:layout_height="40pt"
            android:layout_marginStart="5pt"
            android:onClick="buttonClicked"
            app:backgroundTint="@color/teal_200" />

        <Button
            android:id="@+id/button3"
            android:layout_width="40pt"
            android:layout_height="40pt"
            android:layout_marginStart="5pt"
            android:onClick="buttonClicked"
            app:backgroundTint="@color/teal_200" />
    </TableRow>

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#FFFFFF"
        android:gravity="center">

        <Button
            android:id="@+id/button4"
            android:layout_width="40pt"
            android:layout_height="40pt"
            android:layout_marginStart="5pt"
            android:onClick="buttonClicked"
            app:backgroundTint="@color/teal_200" />

        <Button
            android:id="@+id/button5"
            android:layout_width="40pt"
            android:layout_height="40pt"
            android:layout_marginStart="5pt"
            android:onClick="buttonClicked"
            app:backgroundTint="@color/teal_200" />

        <Button
            android:id="@+id/button6"
            android:layout_width="40pt"
            android:layout_height="40pt"
            android:layout_marginStart="5pt"
            android:onClick="buttonClicked"
            app:backgroundTint="@color/teal_200" />
    </TableRow>

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#FFFFFF"
        android:gravity="center">

        <Button
            android:id="@+id/button7"
            android:layout_width="40pt"
            android:layout_height="40pt"
            android:layout_marginStart="5pt"
            android:onClick="buttonClicked"
            app:backgroundTint="@color/teal_200" />

        <Button
            android:id="@+id/button8"
            android:layout_width="40pt"
            android:layout_height="40pt"
            android:layout_marginStart="5pt"
            android:onClick="buttonClicked"
            app:backgroundTint="@color/teal_200" />

        <Button
            android:id="@+id/button9"
            android:layout_width="40pt"
            android:layout_height="40pt"
            android:layout_marginStart="5pt"
            android:onClick="buttonClicked"
            app:backgroundTint="@color/teal_200" />
    </TableRow>

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center">

        <TextView
            android:id="@+id/win_records"
            android:layout_width="40pt"
            android:layout_height="40pt"
            android:gravity="center"
            android:text="@string/win_records" />
    </TableRow>
</TableLayout>

您也许应该使用“setEnabled”而不是 isClickable?

使用setEnabled属性

代码像

button1.setEnabled = false
button1.isClickable = false

问题是我是在 setContentView() 上面做的

super.onCreate(savedInstanceState)
setContentView(R.layout.activity_online_game)
listenToChangeColors()
if(myValue == 2)
    enableOrDisable(false)

这样做,一切都会好起来的。 :)