LoginSignup
2
4

More than 3 years have passed since last update.

【Kotlin/Android】Listenerのかきかえ

Posted at
main.java
 // idがswitchButtonのSwitchを取得
        Switch switchButton = (Switch) findViewById(R.id.switchButton);
        // switchButtonのオンオフが切り替わった時の処理を設定
        switchButton.setOnCheckedChangeListener(
                new CompoundButton.OnCheckedChangeListener(){
                    public void onCheckedChanged(CompoundButton comButton, boolean isChecked){
                        // オンなら
                        if(isChecked){
                        }
                        // オフなら
                        else{
                        }
                    }
                }
        );
    }
main.kt
switch.setOnCheckedChangeListener { _, isChecked ->
            if (isChecked) {
                // ONの処理

            } else {
                // OFFの処理
            }
        }
2
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
4