一、RadioGroup
是
RadioButton
的组。一个
RadioGroup
里面可以有多个
RadioButton
,但是只能选中其中的一个
RadioButton
。
radio_checkbox.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<RadioGroup
android:id="@+id/genderGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<RadioButton
android:id="@+id/femaleButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/female" />
<RadioButton
android:id="@+id/maleButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/male" />
</RadioGroup>
<CheckBox
android:id="@+id/swim"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/swim" />
<CheckBox
android:id="@+id/run"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/run" />
<CheckBox
android:id="@+id/read"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/read" />
</LinearLayout>
public class RadioCheckBox extends Activity {
//对控件进行声明
private RadioGroup genderGroup = null;
private RadioButton femaleButton = null;
private RadioButton maleButton = null;
private CheckBox swimBox = null;
private CheckBox runBox = null;
private CheckBox readBox = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.radio_checkbox);
genderGroup = (RadioGroup) findViewById(R.id.genderGroup);
femaleButton = (RadioButton) findViewById(R.id.femaleButton);
maleButton = (RadioButton) findViewById(R.id.maleButton);
swimBox = (CheckBox) findViewById(R.id.swim);
runBox = (CheckBox) findViewById(R.id.run);
readBox = (CheckBox) findViewById(R.id.read);
genderGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
/*编写监听器,监听器实现的是RadioGroup.OnCheckedChangedChangeListener()提供的接口,需要重写里面的pubilc void onCheckedChanged(RadioGroup group,int chenkedId)方法,这个方法的第一个参数是用来接收RadioGroup对象,第二个参数是用来接收被选中的RadioButton的ID。*/
public void onCheckedChanged(RadioGroup group, int checkedId) {
if(femaleButton.getId() == checkedId){
System.out.println("female");
//Toast.makeText(RadioCheckBox.this, "female", Toast.LENGTH_SHORT).show();
}else if(maleButton.getId() == checkedId){
System.out.println("male");
}
}
});
/*为每一个CheckBox都编写一个监听器,该监听器实现的是CompoundButton.OnCheckedChangeListener()提供的接口,需要重写里面的public void onCheckedChange(CompoundButton buttonView,boolean isChecked)方法,这个方法的第一个参数是用来接收CompoundButton对象,第二个参数是用来接收是否被选中,在这个方法里面可以做一系列的判断和操作,比如判断某个CheckBox有没有被选中*/
swimBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
System.out.println("swim is checked");
}else{
System.out.println("swim is unchecked");
}
}
});
runBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
System.out.println("run is checked");
}else{
System.out.println("run is unchecked");
}
}
});
readBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
System.out.println("read is checked");
}else{
System.out.println("read is unchecked");
}
}
});
}
}
二、Toast的用法
Toast主要用于提示信息,用起来很方便,一行代码就OK了。
1、创建Toast对象
2、调用show()方法显示。
Toast.makeText(RadioCheckBox.this, "female", Toast.LENGTH_SHORT).show();
三:注意
RadioGroup的监听器实现是RadioGroup.OnCheckedChangeListener()提供的接口,而CheckBox的监听器实现的是CompoundButton.OnCheckedChangeListener()提供的接口。因为:在RadioGroup.OnCheckedChangeListener()里面的onCheckedChanged(RadioGroup group, int checkedId) 方法它里面需要的是一个RadioGroup参数,而RadioGroup并不是CompoundButton的子类,所以它只能再重新写一个这样的接口。而CheckBox它本身就是CompoundButton的子类,所以它可以直接使用这个接口。