禁用Android Studio中的所有按钮,然后重新启动程序

时间:2019-02-18 21:39:39

标签: android kotlin

我是初学者,目前正在制作游戏。游戏结束后,将显示一条消息,显示获胜者。收到此消息后,我想禁用所有按钮。

fun buselect(view: View) {
val buchice = view as Button
var cellid = 0
when (buchice.id) {
    R.id.button -> cellid = 1
    R.id.button2 -> cellid = 2
    R.id.button3 -> cellid = 3
    R.id.button4 -> cellid = 4
    R.id.button5 -> cellid = 5
    R.id.button6 -> cellid = 6
    R.id.button7 -> cellid = 7
    R.id.button8 -> cellid = 8
    R.id.button9 -> cellid = 9
}

var player1 = ArrayList<Int>()
var player2 = ArrayList<Int>()
var activeplayer = 1

fun Playgame(cellid: Int, buchice: Button) {
    if (activeplayer == 1) {
        buchice.text = "x"
        buchice.setBackgroundResource(R.color.blue)
        player1.add(cellid)
        activeplayer = 2

    } else {
        buchice.text = "o"
        buchice.setBackgroundResource(R.color.green)
        player2.add(cellid)
        activeplayer = 1


    }


    buchice.isEnabled = false
    checkwinner()
}

fun checkwinner() {
    var winner = -1
    if (player1.contains(1) && player1.contains(2) && player1.contains(3)) {
        winner = 1

    }
    if (player2.contains(1) && player2.contains(2) && player2.contains(3)) {
        winner = 2
    }
    if (player1.contains(4) && player1.contains(5) && player1.contains(6)) {
        winner = 1
    }
    if (player2.contains(4) && player2.contains(5) && player2.contains(6)) {
        winner = 2
    }
    if (player1.contains(7) && player1.contains(8) && player1.contains(9)) {
        winner = 1
    }
    if (player2.contains(7) && player2.contains(8) && player2.contains(9)) {
        winner = 2
    }
    // clon1

    if (player1.contains(1) && player1.contains(4) && player1.contains(7)) {
        winner = 1

    }
    if (player2.contains(1) && player2.contains(4) && player2.contains(7)) {
        winner = 2
    }
    if (player1.contains(2) && player1.contains(5) && player1.contains(8)) {
        winner = 1
    }
    if (player2.contains(2) && player2.contains(5) && player2.contains(8)) {
        winner = 2
    }
    if (player1.contains(3) && player1.contains(6) && player1.contains(9)) {
        winner = 1
    }
    if (player2.contains(3) && player2.contains(6) && player2.contains(9)) {
        winner = 2
    }
    if (winner != -1) {
        if (winner == 1) {
            Toast.makeText(
                this,
                "player 1 win this sgame ",
                Toast.LENGTH_LONG
            ).show()
        } else {
            Toast.makeText(
                this,
                "player 2 win this sgame",
                Toast.LENGTH_LONG
            ).show()
        }
    }
}
}

问题出在最后一部分,即确定获胜者

        

        <TableRow android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center">
            <Button
                    android:text=""
                    android:layout_width="120dp"
                    android:layout_height="120dp" android:id="@+id/button3"
                    android:background="@android:color/background_light" android:layout_marginRight="10dp"
                    android:layout_marginBottom="10dp" android:onClick="buselect" android:textSize="90sp"/>
            <Button
                    android:text=""
                    android:layout_width="120dp"
                    android:layout_height="120dp" android:id="@+id/button2"
                    android:background="@android:color/background_light" android:layout_marginRight="10dp"
                    android:layout_marginBottom="10dp" android:onClick="buselect" android:textSize="90sp"/>
            <Button
                    android:text=""
                    android:layout_width="120dp"
                    android:layout_height="120dp" android:id="@+id/button"
                    android:background="@android:color/background_light" android:layout_marginBottom="10dp"
                    android:onClick="buselect" android:textSize="90sp"/>
        </TableRow>
        <TableRow android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center">
            <Button
                    android:text=""
                    android:layout_width="120dp"
                    android:layout_height="120dp" android:id="@+id/button6"
                    android:background="@android:color/background_light" android:layout_marginBottom="10dp"
                    android:layout_marginRight="10dp" android:onClick="buselect" android:textSize="90sp"/>
            <Button
                    android:text=""
                    android:layout_width="120dp"
                    android:layout_height="120dp" android:id="@+id/button5"
                    android:background="@android:color/background_light" android:layout_marginBottom="10dp"
                    android:layout_marginRight="10dp" android:onClick="buselect" android:textSize="90sp"/>
            <Button
                    android:text=""
                    android:layout_width="120dp"
                    android:layout_height="120dp" android:id="@+id/button4"
                    android:background="@android:color/background_light" android:layout_marginBottom="10dp"
                    android:onClick="buselect" android:textSize="90sp"/>
        </TableRow>
        <TableRow android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center">
            <Button
                    android:text=""
                    android:layout_width="120dp"
                    android:layout_height="120dp" android:id="@+id/button9"
                    android:background="@android:color/background_light" android:layout_marginRight="10dp"
                    android:onClick="buselect" android:textSize="90sp"/>
            <Button
                    android:text=""
                    android:layout_width="120dp"
                    android:layout_height="120dp" android:id="@+id/button8"
                    android:background="@android:color/background_light" android:layout_marginRight="10dp"
                    android:onClick="buselect" android:textSize="90sp"/>
            <Button
                    android:text=""
                    android:layout_width="120dp"
                    android:layout_height="120dp" android:id="@+id/button7"
                    android:background="@android:color/background_light" android:onClick="buselect"
                    android:textSize="90sp"/>
        </TableRow>
    </TableLayout>
</android.support.constraint.ConstraintLayout>

2 个答案:

答案 0 :(得分:0)

要在Kotlin中禁用按钮,您需要将其指定为 isClickable 属性。例如:

button1.isClickable = true  // Enabled
button2.isClickable = false // Disabled

Java中的示例函数:

  public static void disableButtons(Button b1, Button b2)
  {
      b1.setEnabled(false);
      b2.setEnabled(false);
  }

在Kotlin中的实现:

  fun disableButtons(b1 : Button, b2 : Button) 
  {
      b1.isClickable = false
      b2.isClickable = false
  }

答案 1 :(得分:0)

您想要的是在游戏结束后立即禁用所有按钮以在Kotlin中进行操作

 if(winner != -1){
        Toast.makeText(this,"Game winner is Player $winner",Toast.LENGTH_LONG).show()
        for ( i in 1..9)
        {
            var id:Int = getResources().getIdentifier("button"+i,"id",getPackageName())
            var btn:Button = findViewById(id) as Button
            btn.isClickable = false
            clear()

        }
    }

并使用下面的功能清除您的数据

fun clear()
{
    player1.clear()
    player2.clear()
    activePlayer=1
    for (i in 1..9)
    {
        var id:Int = getResources().getIdentifier("button"+i,"id",getPackageName())
        var btn:Button = findViewById(id) as Button
        btn.isEnabled = true
        btn.setBackgroundColor(Color.LTGRAY)
        btn.text = ""
    }
    count = 0
}