Widgets之RadioButton、RadioGroup和CheckBox

来源:互联网 发布:网络教育网站 编辑:程序博客网 时间:2024/05/18 02:03

1.RadioButton选中不可以取消选择。

2.RadioGroup中包含多个RadioButton,并且使用的onCheckedChangeListener是RadioGroup的onCheckedChangeListener,而不是RadioButtonCompoundButton.OnCheckedChangeListener。这里一定要注意不然会有问题。

rbtn1.setOnCheckedChangeListener(new OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(CompoundButton arg0, boolean arg1) {// TODO Auto-generated method stubToast.makeText(MainActivity.this, "你选中的是"+arg0.getText().toString(), 3333).show();}});

rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(RadioGroup arg0, int arg1) {// TODO Auto-generated method stubif(arg1 == R.id.radioMale){Toast.makeText(MainActivity.this, "你选中的是男", 3333).show();}else if(arg1 == R.id.radioFemale){Toast.makeText(MainActivity.this, "你选中的是女", 3333).show();}}});


3.RadioGroup的默认值为第一个RadioButton。

4.RadioGroup.OnCheckedChangeListener(RadioGroup arg0, int arg1),这里的第一个参数就是被选中的这个RadioGroup,第二个参数非常的重要

它是被选中的RadioButton的id,可以直接和R.id.radiobutton作比较。

5.checkBox选中后可以取消。

cb1.setOnCheckedChangeListener(new OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(CompoundButton arg0, boolean arg1) {// TODO Auto-generated method stubif(arg1){Toast.makeText(MainActivity.this, "你选中了", 3333).show();}else{Toast.makeText(MainActivity.this, "你取消选中了", 3333).show();}}});}

这里的第2个参数就是代表这个checkBox的选中状态,true为选中,false为未选中。

0 0
原创粉丝点击