2

How to implement OnClickListener interface in Kotlin as we do in Java, and handle the ids like switch case? For example, I have two buttons and on click of each button, I have to call a function.

vikas
  • 295
  • 4
  • 16

2 Answers2

4

use the onclickListener interface like this and override its onClick method and you can use WHEN statement as shown! as there is no switch statement in kotlin

Rizwan Atta
  • 3,222
  • 2
  • 19
  • 31
0

1: How to implements OnClickLister

button.setOnClickListner{
//call your funtion
}

2: Switch case

We have when in kotlin instead of a switch. It's just like switch with superpowers.

To know more about when go to this link.

https://kotlinlang.org/docs/reference/control-flow.html

divyanshu bhargava
  • 1,513
  • 1
  • 13
  • 24