2019년 3월 25일 월요일

[Android] Switch widget control

android   Switch widget control sample


import android.widget.Switch;

private Switch mySwitch;

@Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);

  mySwitch = (Switch) findViewById(R.id.mySwitch);

  //set the switch to ON
  mySwitch.setChecked(true);
  //attach a listener to check for changes in state
  mySwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() {

   @Override
   public void onCheckedChanged(CompoundButton buttonView,
     boolean isChecked) {

    if(isChecked){
      // switch on
    }else{
      // switch off
    }
   }
  });
 }

댓글 없음:

댓글 쓰기