<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="@+id/radioGroup">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/rb1"
android:text="男"
android:checked="true"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/rb2"
android:text="女"/>
</RadioGroup>
public class MainActivity extends Activity implements RadioGroup.OnCheckedChangeListener{
private RadioGroup radioGroup;
@Override
protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
radioGroup = (RadioGroup) findViewById(R.id.radioGroup);
radioGroup.setOnCheckedChangeListener(this);
}
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId) {
case R.id.rb1:
Toast.makeText(MainActivity.this,"you are a boy",Toast.LENGTH_SHORT).show();
break;
case R.id.rb2:
Toast.makeText(MainActivity.this,"you are a girl",Toast.LENGTH_SHORT).show();
break;
default:
break;
}
}
}