【问题标题】:onClick function not working with View Binding Android KotlinonClick 函数不适用于 View Binding Android Kotlin
【发布时间】:2021-02-15 13:54:54
【问题描述】:

我是 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 文件添加到您的问题中吗?

标签: android kotlin onclick android-viewbinding


【解决方案1】:

您没有使用 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)
        
    }

【讨论】:

  • 感谢您的回复,但这是我的问题。由于上述方法不起作用,我通过这种方法实现了功能。我在发布时忘记更改代码。问题现已更新。真正的问题是,在布局中注册点击时不会调用 onClick 函数。在使用 Java 之前,我通过实现 onClickListener 接口完成了相同的功能,因此当在布局中注册点击时,它会自动调用覆盖的 onClick 函数。但是当我尝试使用 Kotlin 做同样的事情时,什么都没有发生。
  • 谢谢,但是如果我从后端动态添加这些按钮呢?那么我怎样才能获得点击的任何按钮的 ID?
  • OnClick 将视图转换为按钮后,然后 button.getId() 函数将获取并检查 xml id,如 (button.getId() == R.id.bnOne) 这将给出 id 的状态匹配
  • 是的,但真正面临的问题是,OnClick 函数不会在任何注册的点击上调用。如果按钮是动态的,那么我可能无法为程序中的所有按钮调用 setOnClickListener 方法。
猜你喜欢
  • 2013-05-24
  • 2022-06-15
  • 1970-01-01
  • 1970-01-01
  • 2023-03-17
  • 1970-01-01
  • 1970-01-01
  • 2015-01-20
  • 2019-10-13
相关资源
最近更新 更多