[JAVA] [Kotlin / Android] Listener rewrite

main.java


 //Get Switch with id switchButton
        Switch switchButton = (Switch) findViewById(R.id.switchButton);
        //Set the processing when the switchButton is switched on and off
        switchButton.setOnCheckedChangeListener(
                new CompoundButton.OnCheckedChangeListener(){
                    public void onCheckedChanged(CompoundButton comButton, boolean isChecked){
                        //If on
                        if(isChecked){
                        }
                        //If off
                        else{
                        }
                    }
                }
        );
    }

main.kt


switch.setOnCheckedChangeListener { _, isChecked ->
            if (isChecked) {
                //ON processing

            } else {
                //OFF processing
            }
        }

-Reference site

Recommended Posts

[Kotlin / Android] Listener rewrite
[Android / Kotlin] UI summary
[Android / Kotlin] Detailed notes 2
[Android] Convert Android Java code to Kotlin
[Kotlin / Android] Create a custom view
Convert all Android apps (Java) to Kotlin