简体   繁体   中英

onClick function not working with View Binding Android Kotlin

I am a beginner in kotlin, trying to develop sample projects. I tried using View.OnClickListener interface to get Id of any view clicked in my layout. I have 9 buttons in my layout (tic tac toe game) but when the code is run nothing happens on clicking any buttons, its not even showing any errors.

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 file

<?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>

You didn't attached instance of buttonClickListener with Acitivty View.OnClickListener

For that you need to add line

button.setOnClickListener(this)

Below is implemantation of how to attach onClick listener with button instances

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM