本实例实现了单项选择,多项选择,单项选择按钮必须包括在RadionGroup
--------------AndroidwidgetActivity------------
import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
public class AndroidwidgetActivity extends Activity {
/** Called when the activity is first created. */
private RadioGroup radioSex;
private RadioButton femaleButton;
private RadioButton maleButton;
private CheckBox redCheck;
private CheckBox blueCheck;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//得到main.xml中的控件
radioSex = (RadioGroup) findViewById(R.id.sex);
femaleButton = (RadioButton) findViewById(R.id.female);
maleButton = (RadioButton) findViewById(R.id.male);
redCheck = (CheckBox) findViewById(R.id.red);
blueCheck = (CheckBox) findViewById(R.id.blue);
//给RadioGroup添加监听器
radioSex.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup group, int checkedId) {
if (checkedId == femaleButton.getId()) {
DisplayToast("你选择的是" + femaleButton.getText());
} else {
DisplayToast("你选择的是" + maleButton.getText());
}
}
});
//给CheckBox添加控件
redCheck.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (redCheck.isChecked()) {
DisplayToast("你选择的是" + redCheck.getText());
}
}
});
blueCheck
.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (blueCheck.isChecked()) {
DisplayToast("你选择的是" + blueCheck.getText());
}
}
});
}
/**
* Toast 用法
*
* public static Toast makeText (Context context, int resId, int duration)
*
* context 显示的内容对象 resId 显示的字符串的id duration 显示的时间长度. LENGTH_SHORT 或
* LENGTH_LONG
*
*/
public void DisplayToast(String str) {
// 显示Toast
Toast toast = Toast.makeText(this, str, Toast.LENGTH_LONG);
// 设置Toast显示的位置
toast.setGravity(Gravity.TOP, 0, 220);
toast.show();
}
}
-------------main.xml----------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<RadioGroup
android:id="@+id/sex"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RadioButton
android:id="@+id/female"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Female" />
<RadioButton
android:id="@+id/male"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Male" />
</RadioGroup>
<CheckBox
android:id="@+id/red"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="红色" />
<CheckBox
android:id="@+id/blue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="蓝色" />
</LinearLayout>