Android控件之——RadioGroup、CheckBox
在界面程序开发中,常常使用到单选框和多选框,如:性别选择、单选题、多选题等。以下是Android中单选控件和多选控件的一种用法:
xml文件如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<RadioGroup
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/radioGroup">
<RadioButton
android:id="@+id/male"
android:text="男"/>
<RadioButton
android:id="@+id/female"
android:text="女"/>
<CheckBox
android:id="@+id/checkA"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="A.中国"/>
<CheckBox
android:id="@+id/checkB"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="B.美国"/>
<CheckBox
android:id="@+id/checkC"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="C.德国"/>
<CheckBox
android:id="@+id/checkD"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="D.法国"/>
</RadioGroup>
</LinearLayout>
Java代码如下:
package my.check.namespace;
import android.app.Activity;
import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.Toast;
public class MainActivity extends Activity {
private RadioButton male = null;
private RadioButton female = null;
private RadioGroup group = null;
private CheckBox checkA,checkB,checkC,checkD;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
male = (RadioButton)findViewById(R.id.male);
female = (RadioButton)findViewById(R.id.female);
group = (RadioGroup)findViewById(R.id.radioGroup);
checkA = (CheckBox)findViewById(R.id.checkA);
checkB = (CheckBox)findViewById(R.id.checkB);
checkC = (CheckBox)findViewById(R.id.checkC);
checkD = (CheckBox)findViewById(R.id.checkD);
/*单选框监听器*/
group.setOnCheckedChangeListener(new OnCheckedChangeListener(){
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
if(male.getId() == checkedId){
dispToast("你选择:男");
}else if(female.getId() == checkedId){
dispToast("你选择:女");
}
}
});
/*多选框监听器*/
checkA.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
// TODO Auto-generated method stub
if(isChecked){
dispToast("你选择A.中国");
}else{
dispToast("你取消了选择A");
}
}
});
/*多选框监听器*/
checkB.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
// TODO Auto-generated method stub
if(isChecked){
dispToast("你选择B.美国");
}else{
dispToast("你取消了选择B");
}
}
});
/*多选框监听器*/
checkC.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
// TODO Auto-generated method stub
if(isChecked){
dispToast("你选择C.德国");
}else{
dispToast("你取消了选择C");
}
}
});
/*多选框监听器*/
checkD.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
// TODO Auto-generated method stub
if(isChecked){
dispToast("你选择D.法国");
}else{
dispToast("你取消了选择D");
}
}
});
}
public void dispToast(String text){
Toast toast = Toast.makeText(this, text, Toast.LENGTH_LONG);
toast.show();
}
}