protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.secondactivity);
read = (CheckBox) findViewById(R.id.read);
sport = (CheckBox) findViewById(R.id.sport);
think = (CheckBox) findViewById(R.id.think);
MyCheckBox myCheckBox = new MyCheckBox();
read.setOnCheckedChangeListener(myCheckBox);
sport.setOnCheckedChangeListener(myCheckBox);
think.setOnCheckedChangeListener(myCheckBox);
}
class MyCheckBox implements CompoundButton.OnCheckedChangeListener{
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
CheckBox checkBox = (CheckBox) compoundButton;
switch (checkBox.getId()){
case R.id.read:
Toast.makeText(MainActivity.this,"你选择了读书选项"+b,Toast.LENGTH_SHORT).show();
case R.id.sport:
Toast.makeText(MainActivity.this,"你选择了运动选项"+b,Toast.LENGTH_SHORT).show();
case R.id.think:
Toast.makeText(MainActivity.this, "你选择了发呆选项"+b, Toast.LENGTH_SHORT).show();
}
}
}3个checkbox,添加了checkedchang事件。
我发现,case里面,没有添加break的情况下
第一个read被点击了,会依次弹出 “你选择了读书选项true“+”你选择了运动选项true”+“你选择了发呆选true”
而点第二个sport,则会弹出”你选择了运动选项true”+“你选择了发呆选true”
点最后一个think则只弹出一个“你选择了发呆选true”
相当于,点击前面的按钮,则后面全都会弹出来出来,这是为什么?
望远