View Binding在Android Kotlin中使用onClick函数无效

4

我是一个kotlin的初学者,正在尝试开发一些样例项目。我尝试使用View.OnClickListener接口来获取在我的布局中单击的任何视图的Id。我的布局中有9个按钮(三连棋游戏),但运行代码后单击任何按钮都不会发生任何事情,甚至没有显示任何错误。

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>

你能在你的问题中添加你的XML文件吗? - Tenfour04
1个回答

2

你没有将buttonClickListener实例与Activity View.OnClickListener绑定

为此,你需要添加以下代码:

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)
        
    }

谢谢您的回复,但是这是我的问题。由于上述方法无法工作,我使用了另一种方法来实现功能。我在发布时忘记更改代码。问题现已更新。真正的问题是,当布局中注册点击时,onClick函数不会被调用。我之前使用Java实现了相同的功能,通过实现onClickListener接口,所以每当布局中注册点击时,它将自动调用重写的onClick函数。但是当我尝试使用Kotlin做同样的事情时,什么也没有发生。 - Milan Jayan
谢谢,但如果我从后端动态添加这些按钮,那么我如何获取任何按钮点击的ID? - Milan Jayan
将视图转换为按钮后,单击OnClick,然后使用button.getId()函数获取并检查XML ID是否匹配(如:button.getId() == R.id.bnOne),这将给出ID匹配的状态。 - Sandeep Kumar
是的,我明白了,但我真正面临的问题是,OnClick函数在任何已注册的点击上都没有被调用。如果按钮是动态的,那么我可能无法为程序中的所有按钮调用setOnClickListener方法。 - Milan Jayan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接