Android Study Notes 33: Custom dialog box

Source: Internet
Author: User

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 = "" 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 ;}}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.