RadioGroup 中 RadioButton 单击事件



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();
        }
    }
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值