Я начинаю разработку kotlin, и я делаю простую игру TicTacToe с kotlin, и я хочу проверить, равны ли первые три текста кнопок, и в то же время один из них не пуст, но когда я использую! button1.text.toString () .isEmpty не работает. не знаю почему. Помоги мне. вот мои коды kotlin и xml
package com.example.tictac
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.Toast
import kotlinx.android.synthetic.main.activity_tic_tac.*
class TicTacActivity : AppCompatActivity() {
var isFirstPlayer = true;
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_tic_tac)
init()
}
fun init() {
buttonClick(button1)
buttonClick(button2)
buttonClick(button3)
buttonClick(button4)
buttonClick(button5)
buttonClick(button6)
buttonClick(button7)
buttonClick(button8)
buttonClick(button9)
}
fun buttonClick(button: Button) {
button.setOnClickListener() {
if (isFirstPlayer) {
button.text = "X"
isFirstPlayer = false
} else {
button.text = "O"
isFirstPlayer = true
}
button.isClickable = false
}
if (!button1.text.toString().isEmpty() &&
button1.text.toString().equals(button2.text.toString()) &&
button2.text.toString().equals(button3.text.toString())) {
winner(button1)
}
}
fun winner(button: Button) {
Toast.makeText(this, button.text.toString(), Toast.LENGTH_LONG).show()
}
}
<?xml version = "1.0" encoding = "utf-8"?>
<LinearLayout
xmlns:android = "http://schemas.android.com/apk/res/android"
xmlns:tools = "http://schemas.android.com/tools"
xmlns:app = "http://schemas.android.com/apk/res-auto"
android:layout_width = "match_parent"
android:layout_height = "match_parent"
tools:context = ".TicTacActivity"
android:orientation = "vertical"
>
<LinearLayout android:layout_width = "match_parent"
android:layout_height = "wrap_content"
android:layout_weight = "1">
<Button
android:id = "@+id/button1"
android:layout_width = "wrap_content"
android:layout_height = "match_parent"
android:layout_weight = "1"
android:textSize = "49sp"
/>
<Button
android:id = "@+id/button2"
android:layout_width = "wrap_content"
android:layout_height = "match_parent"
android:layout_weight = "1"
android:textSize = "49sp"
/>
<Button
android:id = "@+id/button3"
android:layout_width = "wrap_content"
android:layout_height = "match_parent"
android:layout_weight = "1"
android:textSize = "49sp"/>
</LinearLayout>
<LinearLayout android:layout_width = "match_parent"
android:layout_height = "wrap_content"
android:layout_weight = "1">
<Button android:id = "@+id/button4"
android:layout_width = "wrap_content"
android:layout_height = "match_parent"
android:layout_weight = "1"
android:textSize = "49sp"/>
<Button android:id = "@+id/button5"
android:layout_width = "wrap_content"
android:layout_height = "match_parent"
android:layout_weight = "1"
android:textSize = "49sp"/>
<Button android:id = "@+id/button6"
android:layout_width = "wrap_content"
android:layout_height = "match_parent"
android:layout_weight = "1"
android:textSize = "49sp"/>
</LinearLayout>
<LinearLayout android:layout_width = "match_parent"
android:layout_height = "wrap_content"
android:layout_weight = "1">
<Button android:id = "@+id/button7"
android:layout_width = "wrap_content"
android:layout_height = "match_parent"
android:layout_weight = "1"
android:textSize = "49sp"/>
<Button android:id = "@+id/button8"
android:layout_width = "wrap_content"
android:layout_height = "match_parent"
android:layout_weight = "1"
android:textSize = "49sp"/>
<Button android:id = "@+id/button9"
android:layout_width = "wrap_content"
android:layout_height = "match_parent"
android:layout_weight = "1"
android:textSize = "49sp"/>
</LinearLayout>
</LinearLayout>

Вы должны включить код, который, по вашему мнению, не работает, в button.setOnClickListener, чтобы он выполнялся каждый раз, когда вы нажимаете кнопку.
Конечно, вам нужно добавить больше кода, чтобы проверить другие случаи.
fun buttonClick(button: Button) {
button.setOnClickListener() {
if (isFirstPlayer) {
button.text = "X"
isFirstPlayer = false
} else {
button.text = "O"
isFirstPlayer = true
}
button.isClickable = false
if (!button1.text.toString().isEmpty() &&
button1.text.toString().equals(button2.text.toString()) &&
button2.text.toString().equals(button3.text.toString())) {
winner(button1)
}
}
}
это работает, большое спасибо. Да, я знаю, что мне нужно проверить другие случаи
Дайте точное определение «не работает». Что вы делаете, чего ожидаете, и что происходит вместо этого?