android - onClick 函数不适用于 View Binding Android Kotlin

标签 android kotlin onclick android-viewbinding

我是 kotlin 的初学者,正在尝试开发示例项目。我尝试使用 View.OnClickListener 接口(interface)来获取在我的布局中单击的任何 View 的 ID。我的布局中有 9 个按钮(井字游戏),但是当代码运行时,单击任何按钮都没有任何 react ,甚至没有显示任何错误。

class MainActivity : AppCompatActivity(), View.OnClickListener {

    private lateinit var binding : ActivityMainBinding
    var active = 1
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)
        
    }

    override fun onClick(view: View?) {
        val button = view as Button
        button.setBackgroundColor(Color.parseColor("#ffffff"))
        if(active == 1) {
            button.text = "X"
            button.setTextColor(Color.parseColor("#FF6200EE"))
            button.textSize = 20f
            button.isEnabled = false
            active = 0
        }
        else {
            button.text = "O"
            button.setTextColor(Color.parseColor("#FF6200EE"))
            button.textSize = 20f
            button.isEnabled = false
            active = 1
        }
    }
}
xml文件
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context=".MainActivity"
    android:background="#F6F6F6">

    <TableLayout
        android:id="@+id/tlTableLayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent">

        <TableRow
            android:id="@+id/trRow1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

            <Button
                android:id="@+id/bnOne"
                android:layout_width="40pt"
                android:layout_height="40pt"
                android:layout_marginRight="5pt"/>

            <Button
                android:id="@+id/bnTwo"
                android:layout_width="40pt"
                android:layout_height="40pt"
                android:layout_marginRight="5pt"/>

            <Button
                android:id="@+id/bnThree"
                android:layout_width="40pt"
                android:layout_height="40pt"/>

        </TableRow>

        <TableRow
            android:id="@+id/trRow2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

            <Button
                android:id="@+id/bnFour"
                android:layout_width="40pt"
                android:layout_height="40pt"
                android:layout_marginRight="5pt"/>

            <Button
                android:id="@+id/bnFive"
                android:layout_width="40pt"
                android:layout_height="40pt"
                android:layout_marginRight="5pt"/>

            <Button
                android:id="@+id/bnSix"
                android:layout_width="40pt"
                android:layout_height="40pt"/>

        </TableRow>

        <TableRow
            android:id="@+id/trRow3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

            <Button
                android:id="@+id/bnSeven"
                android:layout_width="40pt"
                android:layout_height="40pt"
                android:layout_marginRight="5pt"/>

            <Button
                android:id="@+id/bnEight"
                android:layout_width="40pt"
                android:layout_height="40pt"
                android:layout_marginRight="5pt"/>

            <Button
                android:id="@+id/bnNine"
                android:layout_width="40pt"
                android:layout_height="40pt"/>

        </TableRow>

    </TableLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

最佳答案

您没有使用 Acitivty View.OnClickListener 附加 buttonClickListener 的实例
为此,您需要添加行

button.setOnClickListener(this)
下面是如何使用按钮实例附加 onClick 监听器的实现
override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)

        binding.bnOne.setOnClickListener(this)
        binding.bnTwo.setOnClickListener(this)
        ................
        ................
        ................
        binding.bnNine.setOnClickListener(this)
        
    }

关于android - onClick 函数不适用于 View Binding Android Kotlin,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66209341/

相关文章:

image - Glide 文件未找到异常

kotlin - 如何在 Kotlin 中为多个变量分配相同的值

javascript - 在Jquery中定义一个按钮的onclick事件

javascript - JS在li上使用onclick事件显示 block

android - 找不到与给定名称匹配的资源 'android:Theme.Material.Light.DarkActionBar'

java - 执行 aapt 时出错 : Return code -1073741819 (Eclipse)

android - 为什么某些 Android Studio 类会以不同的颜色突出显示?

如果没有 Provides-annotated 方法,则无法提供 Android dagger

android - 如何检测 GestureDetector.SimpleOnGestureListener 中的 View

javascript - 函数在第一次点击后表现异常