Android: Common Alertdialog dialog boxes and custom dialogs

Source: Internet
Author: User

Common dialog have confirmation dialog, radio button dialog, multi-select button dialog, Check Button dialog box and custom dialog box

Common methods of Alertdialog

Settitle: Setting a title for a dialog box

Setmessage: Setting Content for dialog boxes

SetIcon: Setting icons for dialog boxes

Setitems Settings dialog box to display the list

Setmultichoiceitems: Generally used for check box display

Setsinglechoiceitem:, Set radio button

Setneutralbutton: Normal button

Setpositivebutton: Add OK button

Setnegativebutton: Add Cancel button

Setview: Setting Custom Styles

Here's an example of how these methods


This is the result of the operation:

Mainactivity.class

Package Com.example.alertdialog;import Android. R.bool;import Android.os.bundle;import Android.app.activity;import Android.app.alertdialog;import Android.app.alertdialog.builder;import Android.app.dialog;import Android.content.dialoginterface;import Android.content.pm.labeledintent;import Android.view.layoutinflater;import Android.view.view;import Android.view.view.onclicklistener;import Android.widget.button;import Android.widget.toast;public Class Mainactivity extends Activity implements Onclicklistener {Private button button1;private button button2;private button bu Tton3;private button button4;private button button5;private int mark=0;private String item[] = {"Student", "worker", "teacher", "Farmer"}; Private String multchoice[]={"Travel", "movie", "Sport", "reading"}; @Overrideprotected void OnCreate (Bundle savedinstancestate) { Super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); button1 = (Button) Findviewbyid ( R.id.btn_button1) button2 = (Button) Findviewbyid (r.id.btn_button2); button3 = (Button)Findviewbyid (r.id.btn_button3); button4 = (Button) Findviewbyid (r.id.btn_button4); button5 = (Button) Findviewbyid ( R.ID.BTN_BUTTON5); Button1.setonclicklistener (this), Button2.setonclicklistener (this); Button3.setonclicklistener (this), Button4.setonclicklistener (This), Button5.setonclicklistener (this);}  @Overridepublic void OnClick (View v) {//TODO auto-generated method Stubswitch (V.getid ()) {//confirmation dialog box Case R.id.btn_button1: {Alertdialog.builder builder = new Builder (this); Builder.seticon (R.drawable.ic_launcher); Builder.settitle (Confirmation dialog box) ; Builder.setmessage ("Confirm exit?") Builder.setpositivebutton ("OK", new Dialoginterface.onclicklistener () {@Overridepublic void OnClick ( Dialoginterface dialog, int which) {//TODO auto-generated method Stubtoast.maketext (Mainactivity.this, "You clicked OK button", Toast.length_short). Show ();}}); Builder.setnegativebutton ("Cancel", new Dialoginterface.onclicklistener () {@Overridepublic void OnClick (dialoginterface dialog, int which) {//TODO auto-generated method Stubtoast.maketext (MainactivIty.this, "You clicked the Cancel button", Toast.length_short). Show ();}); Builder.create (); Builder.show (); break;} List dialog box R.id.btn_button2: {alertdialog.builder builder = new Builder (this); Builder.seticon (r.drawable.ic_ launcher); Builder.settitle ("occupation"); Builder.setitems (item, new Dialoginterface.onclicklistener () {@Overridepublic void OnClick (Dialoginterface dialog, int which) {//TODO auto-generated method Stubtoast.maketext (Mainactivity.this, " Your occupation is "+ Item[which],toast.length_short". Show ();}}); Builder.create (); Builder.show (); break;} Multi-Select dialog box case R.id.btn_button3: {final boolean choose[]=new boolean[multchoice.length]; Alertdialog.builder builder = new Builder (this); Builder.seticon (R.drawable.ic_launcher); Builder.settitle ("hobby"); Builder.setmultichoiceitems (Multchoice, NULL, new Dialoginterface.onmultichoiceclicklistener () {@Overridepublic void OnClick (Dialoginterface dialog, int which, Boolean isChecked) {//TODO auto-generated method Stubchoose[which]=ische cked;}}); Builder.setpositivebutton ("Confirm", new DialogInterface.onclicklistener () {@Overridepublic void OnClick (dialoginterface dialog, int which) {//TODO auto-generated Method stubstring result= ""; for (int i=0;i<multchoice.length;i++) {if (Choose[i]) {result+=multchoice[i]+ "";}} Toast.maketext (Mainactivity.this, "Your hobby [" +result+ "]", Toast.length_short). Show ();}); Builder.create (); Builder.show (); break;} Radio dialog box Case R.ID.BTN_BUTTON4: {mark=0; Alertdialog.builder builder = new Builder (this); Builder.seticon (R.drawable.ic_launcher); Builder.settitle ("occupation"); Builder.setsinglechoiceitems (item, 0, new Dialoginterface.onclicklistener () {@Overridepublic void OnClick ( Dialoginterface dialog, int which) {//TODO auto-generated method Stubmark=which;}}); Builder.setpositivebutton ("OK", new Dialoginterface.onclicklistener () {@Overridepublic void OnClick (dialoginterface dialog, int which) {//TODO auto-generated method Stubtoast.maketext (Mainactivity.this, "Your Occupation:" +item[mark], Toast.length_short). Show ();}}); Builder.create (); Builder.show (); break;} Custom dialog box Case R.ID.btn_button5: {layoutinflater inflater=layoutinflater.from (this); View view=inflater.inflate (R.layout.item, NULL); Alertdialog.builder builder = new Builder (this); Builder.seticon (R.drawable.ic_launcher); Builder.settitle (Customize dialog box) ; Builder.setview (view); Builder.setneutralbutton ("Normal button", new Dialoginterface.onclicklistener () {@Overridepublic void OnClick (Dialoginterface dialog, int which) {//TODO auto-generated method Stubtoast.maketext (Mainactivity.this, " I am a custom dialog box Oh ", Toast.length_short). Show ();}}); Builder.create (); Builder.show (); break;}}}

Layout file

Activity_main.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 "> <linearlayout android:layout_width=" fill_parent "android:layout_height=" Wrap_conten T "android:orientation=" vertical "> <button android:id=" @+id/btn_button1 "Androi        D:layout_width= "Fill_parent" android:layout_height= "wrap_content" android:text= "confirmation Dialog"/> <button android:id= "@+id/btn_button2" android:layout_width= "Fill_parent" android:layou t_height= "wrap_content" android:text= "list pairBox "/> <button android:id=" @+id/btn_button3 "android:layout_width=" Fill_parent " android:layout_height= "wrap_content" android:text= "Multiple selection Dialog"/> <button android:id= "@+ Id/btn_button4 "android:layout_width=" fill_parent "android:layout_height=" Wrap_content "a ndroid:text= "Radio dialog box"/> <button android:id= "@+id/btn_button5" android:layout_width= "fill _parent "android:layout_height=" wrap_content "android:text=" Custom dialog box "/> &LT;/LINEARLAYOUT&GT;&L T;/relativelayout>

Customizing the dialog Layout file

Item.xml

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "    android:layout_width=" match_parent "    android:layout_height=" match_parent "    android:o rientation= "vertical" >    <imageview         android:layout_width= "wrap_content"        android:layout_height= "Wrap_content"        android:scaletype= "Fitcenter"        android:src= "@drawable/icon"        />    <textview         android:layout_width= "fill_parent"        android:layout_height= "wrap_content"        android:text= " I am a custom dialog box "        /></linearlayout>

I hope we can help you. Slag Residue study 、、、

Android: Common Alertdialog dialog boxes and custom dialogs

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.