Radio is composed of RadioGroup and RadioButton. This is very simple.
Main. 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">
<TextView
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "your gender: select the answer"/>
<RadioGroup
Android: id = "@ + id/rg1"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: orientation = "vertical"
Android: layout_x = "3px"
Android: layout_y = "54px"
>
<RadioButton
Android: id = "@ + id/rb1"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "boys"
/>
<RadioButton
Android: id = "@ + id/rb2"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = ""
/>
</RadioGroup>
<Button
Android: id = "@ + id/btn"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "test"
/>
</LinearLayout>
Code
Package zziss. android. radiotest;
Import android. app. Activity;
Import android. OS. Bundle;
Import android. view. View;
Import android. widget. Button;
Import android. widget. RadioButton;
Import android. widget. RadioGroup;
Import android. widget. Toast;
Public class RadioTestActivity extends Activity {
/** Called when the activity is first created .*/
RadioGroup rg1;
RadioButton rb1;
RadioButton rb2;
Button ibtn;
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
Rg1 = (RadioGroup) this. findViewById (R. id. rg1 );
Rb1. = (RadioButton) this. findViewById (R. id. rb1 );
Rb2 = (RadioButton) this. findViewById (R. id. rb2 );
Rg1.setOnCheckedChangeListener (new RadioGroup. OnCheckedChangeListener (){
@ Override
Public void onCheckedChanged (RadioGroup group, int checkedId ){
// TODO Auto-generated method stub
If (checkedId = rb1.getId ())
{
ShowInfo (rb1.getText (). toString ());
}
If (checkedId = rb2.getId ())
{
ShowInfo (rb2.getText (). toString ());
}
}
});
Ibtn = (Button) this. findViewById (R. id. btn );
Ibtn. setOnClickListener (new View. OnClickListener (){
@ Override
Public void onClick (View v ){
// TODO Auto-generated method stub
Int id = rg1.getCheckedRadioButtonId ();
If (id! =-1)
ShowIdText (id );
Else
ShowInfo ("select ");
}
});
}
Private void ShowInfo (String str)
{
Toast toast = Toast. makeText (this, str, Toast. LENGTH_SHORT );
Toast. show ();
}
Private void showIdText (int aId)
{
ShowInfo (RadioButton) this. findViewById (aId). getText (). toString ());
}
}