Implementation of the Android Development Alertdialog.builder dialog box

Source: Internet
Author: User

ANDROIDAPI provides a dialog dialog box control, but Google clearly states that developers are not advised to use dialog to create dialogs, but should customize the dialog box or use the dialog subclasses provided in the API, such as Alertdialog. Next record the specific use of alertdialog, for the vast number of beginners to learn, but also for their own future inspection.

First, write a button in the resource file XML.

<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools "android:layout_width=" match_parent "android:layout_height=" Match_parent "Android:paddi ngbottom= "@dimen/activity_vertical_margin" android:paddingleft= "@dimen/activity_horizontal_margin" Android: paddingright= "@dimen/activity_horizontal_margin" android:paddingtop= "@dimen/activity_vertical_margin" tools: Context= ". Mainactivity "> <button android:id=" @+id/button1 "android:layout_width=" Wrap_content "Androi        d:layout_height= "Wrap_content" android:layout_centerhorizontal= "true" android:layout_centervertical= "true"         android:text= "warning dialog box"/> <button android:id= "@+id/button2" android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" android:layout_alignleft= "@+id/button1" Android:layout_alignrig ht= "@+id/button1" android:layout_below= "@+id/Button1 "android:layout_margintop=" 17DP "android:text=" select "/> <button android:id=" @+id/butto N3 "Android:layout_width=" Wrap_content "android:layout_height=" Wrap_content "Android:layout_alignleft = "@+id/button2" android:layout_alignright= "@+id/button2" android:layout_below= "@+id/button2" Android: layout_margintop= "23DP" android:text= "Single-selection dialog box"/></relativelayout>
    • Create a simple prompt window

In the activity, declare the global variable Alertdialog window object Builder Alertdialog.builder Builder, and instantiate this Window object Builder new Alertdialog.builder in the OnCreate method ( This).

The API provides a fairly rich approach to this object.

such as: Builder.settitile ("set window title");

Bulder.setmessage ("Set window Contents");

Builder.seticon ("Set window Icon");

......

Create a Builder registration related button for the window:

"Cancel", "OK", "ignore" and other related buttons, because it is learning, the button's listener events just do a simple toast processing, in normal development is to carry out business logic processing.

    

1         //Register The Click event via the Dialoginterface interface by registering the "OK" button with the Window object Builder2Builder.setpositivebutton ("OK",NewDialoginterface.onclicklistener () {3             4 @Override5              Public voidOnClick (Dialoginterface Dialog,intwhich) {6                 //TODO auto-generated Method Stub7                 //complete the writing of business logic code8Toast.maketext (mainactivity. This, "OK delete!" ", 1). Show ();9             }Ten         }); One         //Register A Click event via the Dialoginterface interface by registering the "Cancel" button with the Window object Builder ABuilder.setnegativebutton ("Cancel",NewDialoginterface.onclicklistener () { - @Override -              Public voidOnClick (Dialoginterface Dialog,intwhich) { the                 //TODO auto-generated Method Stub -Toast.maketext (mainactivity. This, "Cancel the operation! ", 1). Show (); -             } -         }); +         //Register The Click event via the Dialoginterface interface by registering the "Ignore" button with the Window object Builder -Builder.setneutralbutton ("Ignore",NewDialoginterface.onclicklistener () { + @Override A              Public voidOnClick (Dialoginterface Dialog,intwhich) { at                 //TODO auto-generated Method Stub -Toast.maketext (mainactivity. This, "Ignore the operation! ", 1). Show (); -             } -         }); -Button.setonclicklistener (NewOnclicklistener () { - @Override in              Public voidOnClick (view view) { -                 //TODO auto-generated Method Stub to //Toast.maketext (Mainactivity.this, "Who made you click?") Tease ", 1). Show (); +                 //when the window is built, the display window - builder.show (); the             } *});
    • Create a multi-select window

 Register the Click event via the button, initialize and instantiate the Window object creator in the Click event: Alertdialog.builder Builder = new Alertdialog.builder (mainactivity.this); The following actions create a simple dialog box that is basically the same.

    

1Button2.setonclicklistener (NewOnclicklistener () {2 @Override3              Public voidOnClick (View v) {4                 //TODO auto-generated Method Stub5Alertdialog.builder Builder =NewAlertdialog.builder (mainactivity. This);6Builder.settitle ("Choose Hobby");7                 Finalstring[] Hobby = {"Surfing the Internet", "chatting", "playing games"} ;8Builder.setmultichoiceitems (Hobby,New Boolean[]{true,false,true},NewDialoginterface.onmultichoiceclicklistener () {9 @OverrideTen                      Public voidOnClick (Dialoginterface Dialog,intWhich,BooleanisChecked) { One                         //TODO auto-generated Method Stub A                         if(isChecked) { -Toast.maketext (mainactivity. This, "select---->" +hobby[which], 1). Show (); -}Else{ theToast.maketext (mainactivity. This, "Cancel---->" +hobby[which], 1). Show (); -                         } -                     } -                 }); +Builder.setpositivebutton ("Confirm",NewDialoginterface.onclicklistener () { - @Override +                      Public voidOnClick (Dialoginterface Dialog,intwhich) { A                         //TODO auto-generated Method Stub at                     } -                 }); -Builder.setnegativebutton ("Cancel",NewDialoginterface.onclicklistener () { - @Override -                      Public voidOnClick (Dialoginterface Dialog,intwhich) { -                         //TODO auto-generated Method Stub in                     } -                 }); to                 //when the window is built, the display window + builder.show (); -             } the});
    • Create a single-selection window

 Register the Click event via the button, initialize and instantiate the Window object creator in the Click event: Alertdialog.builder Builder = new Alertdialog.builder (mainactivity.this); The subsequent operation is basically consistent with creating a multi-select window.

1Button3.setonclicklistener (NewOnclicklistener () {2 @Override3              Public voidOnClick (View v) {4                 //TODO auto-generated Method Stub5Alertdialog.builder Builder =NewAlertdialog.builder (mainactivity. This);6Builder.settitle ("Choose Hobby");7                 Finalstring[] Hobby = {"Surfing the Internet", "chatting", "playing games"} ;8                 //Window Object Creator can put adapter container9 //Builder.setadapter (adapter, listener);Ten                 //Create a list of options for the Window object Builder, if none is selected, the parameter is:-1 OneBuilder.setsinglechoiceitems (Newstring[]{"Internet", "chat", "Play games"},  A-1,NewDialoginterface.onclicklistener () { - @Override -                              Public voidOnClick (Dialoginterface Dialog,intwhich) { theToast.maketext (mainactivity. This, "You have chosen-" "+hobby[which", 1). Show (); -                             } -                         }); -                 //Register The Click event via the Dialoginterface interface by registering the "OK" button with the Window object Builder +Builder.setpositivebutton ("OK",NewDialoginterface.onclicklistener () { - @Override +                      Public voidOnClick (Dialoginterface Dialog,intwhich) { A                          at                     } -                 }); -                 //Register The Click event via the Dialoginterface interface by registering the "Ignore" button with the Window object Builder -Builder.setneutralbutton ("Ignore",NewDialoginterface.onclicklistener () { - @Override -                      Public voidOnClick (Dialoginterface Dialog,intwhich) { in                      -                     } to                 }); +                 //Register A Click event via the Dialoginterface interface by registering the "Cancel" button with the Window object Builder -Builder.setnegativebutton ("Cancel",NewDialoginterface.onclicklistener () { the @Override *                      Public voidOnClick (Dialoginterface Dialog,intwhich) { $                         Panax Notoginseng                     } -                 }); the                 //when the window is finished, display the window + builder.show (); A             } the});

Implementation of the Android Development Alertdialog.builder dialog box

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: info-contact@alibabacloud.com 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.