<?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" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/title" /> <RadioGroup android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/radioGroup" > <RadioButton android:text="@string/red" android:id="@+id/radioButton_1" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <RadioButton android:text="@string/green" android:id="@+id/radioButton_2" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <RadioButton android:text="@string/blue" android:id="@+id/radioButton_3" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </RadioGroup> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="" android:id="@+id/textTip" /> </LinearLayout>
public class RadioButtonActivity extends Activity {
/** Called when the activity is first created. */
private RadioGroup radioGroup;
private RadioButton radioButton_1;
private RadioButton radioButton_2;
private RadioButton radioButton_3;
private TextView textTip;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
radioGroup = (RadioGroup) findViewById(R.id.radioGroup);
radioButton_1 = (RadioButton) findViewById(R.id.radioButton_1);
radioButton_2 = (RadioButton) findViewById(R.id.radioButton_2);
radioButton_3 = (RadioButton) findViewById(R.id.radioButton_3);
textTip = (TextView) findViewById(R.id.textTip);
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
textTip.setText("选择ID:"+checkedId);
}
});
}
}