From: http://www.cnblogs.com/linjiqin/archive/2011/03/10/1980170.html
Android radio button dialog box
Main. xml layout File
<? 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">
<Edittext Android: text = ""
Android: Id = "@ + ID/edittext"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: editable = "false"
Android: cursorvisible = "false"/>
<Button Android: text = "show radio dialog box"
Android: Id = "@ + ID/button"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"/>
</Linearlayout>
Array. xml Array
<? XML version = "1.0" encoding = "UTF-8"?>
<Resources>
<String-array name = "holobby">
<Item> swimming </item>
<Item> playing basketball </item>
<Item> mountaineering </item>
</String-array>
</Resources>
Alertdialog class
Package com. ljq. Dialog;
Import Android. App. activity;
Import Android. App. Dialog;
Import Android. App. alertdialog. Builder;
Import Android. content. dialoginterface;
Import Android. content. dialoginterface. onclicklistener;
Import Android. OS. Bundle;
Import Android. View. view;
Import Android. widget. Button;
Import Android. widget. edittext;
Publicclass alertdialog extends activity {
Private edittext;
Privatefinalstaticint dialog = 1;
@ Override
Publicvoid oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. Main );
Edittext = (edittext) findviewbyid (R. Id. edittext );
Button button = (button) findviewbyid (R. Id. Button );
Button. setonclicklistener (New View. onclicklistener (){
Publicvoid onclick (view v ){
// Display dialog box
Showdialog (DIALOG );
}
});
}
/**
* Create radio button dialog box
*/
@ Override
Protected dialog oncreatedialog (int id ){
Dialog dialog = NULL;
Switch (ID ){
Case dialog:
Builder = new Android. App. alertdialog. Builder (this );
// Set the icon in the dialog box
Builder. seticon (R. drawable. header );
// Set the title of the dialog box
Builder. settitle ("single choice button dialog box ");
// 0: the first radio button is selected by default.
Builder. setsinglechoiceitems (R. array. holobby, 0, new onclicklistener (){
Publicvoid onclick (dialoginterface dialog, int which ){
String hoddy = getresources (). getstringarray (R. array. Hoby) [which];
Edittext. settext ("You selected:" + hoddy );
}
});
// Add a confirmation button
Builder. setpositivebutton ("OK", new dialoginterface. onclicklistener (){
Publicvoid onclick (dialoginterface dialog, int which ){
}
});
// Create a single choice button dialog box
Dialog = builder. Create ();
Break;
}
Return dialog;
}
}
Running result
Main. xml layout File
<? 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">
<Edittext Android: text = ""
Android: Id = "@ + ID/edittext"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: editable = "false"
Android: cursorvisible = "false"/>
<Button Android: text = "show radio dialog box"
Android: Id = "@ + ID/button"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"/>
</Linearlayout>
Array. xml Array
<? XML version = "1.0" encoding = "UTF-8"?>
<Resources>
<String-array name = "holobby">
<Item> swimming </item>
<Item> playing basketball </item>
<Item> mountaineering </item>
</String-array>
</Resources>
Alertdialog class
Package com. ljq. Dialog;
Import Android. App. activity;
Import Android. App. Dialog;
Import Android. App. alertdialog. Builder;
Import Android. content. dialoginterface;
Import Android. content. dialoginterface. onclicklistener;
Import Android. OS. Bundle;
Import Android. View. view;
Import Android. widget. Button;
Import Android. widget. edittext;
Publicclass alertdialog extends activity {
Private edittext;
Privatefinalstaticint dialog = 1;
@ Override
Publicvoid oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. Main );
Edittext = (edittext) findviewbyid (R. Id. edittext );
Button button = (button) findviewbyid (R. Id. Button );
Button. setonclicklistener (New View. onclicklistener (){
Publicvoid onclick (view v ){
// Display dialog box
Showdialog (DIALOG );
}
});
}
/**
* Create radio button dialog box
*/
@ Override
Protected dialog oncreatedialog (int id ){
Dialog dialog = NULL;
Switch (ID ){
Case dialog:
Builder = new Android. App. alertdialog. Builder (this );
// Set the icon in the dialog box
Builder. seticon (R. drawable. header );
// Set the title of the dialog box
Builder. settitle ("single choice button dialog box ");
// 0: the first radio button is selected by default.
Builder. setsinglechoiceitems (R. array. holobby, 0, new onclicklistener (){
Publicvoid onclick (dialoginterface dialog, int which ){
String hoddy = getresources (). getstringarray (R. array. Hoby) [which];
Edittext. settext ("You selected:" + hoddy );
}
});
// Add a confirmation button
Builder. setpositivebutton ("OK", new dialoginterface. onclicklistener (){
Publicvoid onclick (dialoginterface dialog, int which ){
}
});
// Create a single choice button dialog box
Dialog = builder. Create ();
Break;
}
Return dialog;
}
}
Running result