public class Main3Activity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener{
private CheckBox checkBox_ball,checkBox_run,checkBox_code;
private RadioGroup radioGroup;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
checkBox_ball = (CheckBox) findViewById(R.id.checkBox_ball);
checkBox_run = (CheckBox) findViewById(R.id.checkBox2_run);
checkBox_code = (CheckBox) findViewById(R.id.checkBox3_code);
checkBox_ball.setOnCheckedChangeListener(this);
checkBox_run.setOnCheckedChangeListener(this);
checkBox_code.setOnCheckedChangeListener(this);
//单选分组
radioGroup = (RadioGroup) findViewById(R.id.radioGroup);
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
//group RadioGroup
//checkedId 已经被选中的RadioButton的ID
//从单选分组里获取当前被选中的单选按钮
RadioButton rb = (RadioButton) radioGroup.findViewById(checkedId);
Toast.makeText(Main3Activity.this, rb.getText(), Toast.LENGTH_SHORT).show();
}
});
}
public void submit(View view){
//通过按钮单击事件来获取复选框的值
ArrayList<String> item = new ArrayList<>();
if(checkBox_ball.isChecked()){
item.add(checkBox_ball.getText().toString());
}
if(checkBox_run.isChecked()){
item.add(checkBox_run.getText().toString());
}
if(checkBox_code.isChecked()){
item.add(checkBox_code.getText().toString());
}
Toast.makeText(Main3Activity.this, item.toString(), Toast.LENGTH_SHORT).show();
}
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
String text = buttonView.getText().toString();
if(isChecked){
Toast.makeText(Main3Activity.this, text+" 被选中", Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(Main3Activity.this, text+" 被取消", Toast.LENGTH_SHORT).show();
}
}
}