When using the dialog box in Android, you can use the built-in dialog box or define a dialog box by yourself. This program uses a custom dialog box with single-choice images. Click "select your favorite album". This dialog box is displayed.
Define the style of the radio list in the dialog box
<? 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 = "horizontal"> <! -- Define an imageview as part of the list item. --> <Imageview Android: Id = "@ + ID/Header" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: paddingleft = "10dp"/> <! -- Defines a textview that is used as part of the list item. --> <Textview Android: Id = "@ + ID/Name" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: gravity = "center_vertical" Android: paddingleft = "10dp" Android: textcolor = "# ff000066" Android: textsize = "16dp"/> </linearlayout>
Override oncreatedialog to define our own dialog box.
Public class customlistdialog extends activity {final int list_dialog = 0x113; // define the names of the three list items private string [] names = new string [] {"kite ", "Perfect Day", "Time"}; // defines the private int [] imageids = new int [] {R. drawable. sunyz_1, R. drawable. sunyz_6, R. drawable. sunyz_7}; @ overridepublic void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); button bn = (button) findviewbyid (R. id. BN); // bind the event listener bn to the button. setonclicklistener (new view. onclicklistener () {@ overridepublic void onclick (View Source) {// display dialog box showdialog (list_dialog );}});} // override the oncreatedialog method creation dialog box @ overridepublic dialog oncreatedialog (int id, bundle state) {// determine which type of dialog box to generate switch (ID) {Case list_dialog: builder B = new alertdialog. builder (this); // set the icon B in the dialog box. seticon (R. drawable. tools); // set the dialog box Title B. settitle ("Single-choice List dialog box"); // create a list object. The list object element is maplist <Map <string, Object> listitems = new arraylist <Map <string, object> (); For (INT I = 0; I <names. length; I ++) {Map <string, Object> listitem = new hashmap <string, Object> (); listitem. put ("CDS", imageids [I]); listitem. put ("cdname", Names [I]); listitems. add (listitem) ;}// create a simpleadaptersimpleadapter simpleadapter = new simpleadapter (this, listitems, R. layout. row, new string [] {"cdname", "CDS"}, new int [] {R. id. name, R. id. header}); // set multiple list B for the dialog box. setadapter (simpleadapter // sets the listener for the Click Event of the list item, new dialoginterface. onclicklistener () {@ overridepublic void onclick (dialoginterface dialog, int which) {textview show = (textview) findviewbyid (R. id. show); // which indicates which list item is clicked show. settext ("your favorite album is:" + Names [which]) ;}}); // create dialog box return B. create () ;}return null ;}}