1. 程式人生 > >android studio switch控制元件使用

android studio switch控制元件使用

先使用toast小試牛刀:

Switch sc=(Switch)findViewById(R.id.switch1);
sc.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    @Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
        if (b){
            Toast.makeText(getApplicationContext(),"on",Toast.LENGTH_SHORT
).show(); }else { Toast.makeText(getApplicationContext(),"off",Toast.LENGTH_SHORT).show(); } } });