1.控件属性:
<RadioGroup
android:orientation="horizontal"
android:id="@+id/radioGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RadioButton
android:id="@+id/radio0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="@string/radio_button1" />
<RadioButton
android:id="@+id/radio1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/radio_button2" />
</RadioGroup>
2.控件使用:
public class RadioButtonTest extends Activity implements OnCheckedChangeListener {
private RadioGroup rg;
private RadioButton rb1;
private RadioButton rb2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_radiobutton);
rg = (RadioGroup)findViewById(R.id.radioGroup1);
rb1 =(RadioButton)findViewById(R.id.radio0);
rb2 =(RadioButton)findViewById(R.id.radio1);
rg.setOnCheckedChangeListener(this);
}
@Override
public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {
switch(checkedId){
case R.id.radio0:
Log.i("tag",rb1.getText()+",你是被神灵选中的孩子!");
break;
case R.id.radio1:
Log.i("tag",rb2.getText()+",你是被魔鬼选中的孩子!");
break;
default:;
}
}
}