android.widget.Switch - on/off event listener?

前端 未结 11 1498
庸人自扰
庸人自扰 2020-12-02 04:56

I would like to implement a switch button, android.widget.Switch (available from API v.14).



        
11条回答
  •  失恋的感觉
    2020-12-02 05:38

    Use the following snippet to add a Switch to your layout via XML:

    
    

    Then in your Activity's onCreate method, get a reference to your Switch and set its OnCheckedChangeListener:

    Switch onOffSwitch = (Switch)  findViewById(R.id.on_off_switch); 
    onOffSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        Log.v("Switch State=", ""+isChecked);
    }       
    
    });
    

提交回复
热议问题