Android's Dialog detailed

Source: Internet
Author: User

There are five types of dialogs in Android: The General Dialog, the list dialog, the radio button dialog, the Multi-Select button dialog, and the Customize dialog box.

In the actual development, with the system dialog will be very few, because it is too ugly, the artist does not want to, most of the use of custom dialog box. Of course, to learn the system, the customization is simple, so we first to learn the system, behind the writing of a custom dialog box.

General dialog box:

Not much to say first:

Code:

private void Dialog1 () {Alertdialog.builder builder=new alertdialog.builder (this); First get the constructor builder.settitle ("hint"); Set the title Builder.setmessage ("Confirm exit?"); Set content Builder.seticon (r.mipmap.ic_launcher);//set icon, image ID can be Builder.setpositivebutton ("OK", new Dialoginterfa Ce.                Onclicklistener () {//Set OK button @Override public void OnClick (Dialoginterface dialog, int which) { Dialog.dismiss ();            Close dialog Toast.maketext (mainactivity.this, "confirm" + which, Toast.length_short). Show ();        }        }); Builder.setnegativebutton ("Cancel", new Dialoginterface.onclicklistener () {//Set Cancel button @Override public vo                ID OnClick (dialoginterface dialog, int which) {Dialog.dismiss ();            Toast.maketext (Mainactivity.this, "Cancel" + which, Toast.length_short). Show ();        }        });            Builder.setneutralbutton ("Ignore", new Dialoginterface.onclicklistener () {//Set Ignore button@Override public void OnClick (Dialoginterface dialog, int which) {Dialog.dismiss ();            Toast.maketext (mainactivity.this, "ignore" + which, Toast.length_short). Show ();        }        });    The parameters are set to complete, created and displayed Builder.create (). Show (); }

Note: The code on the comments have been compared to all, I believe we can read! Dialog can set three selection buttons and specify the button response event when set.

Do you think three buttons write three times to respond to events, very cumbersome? Note: There is a which in the onclick parameter, what does this mean? This which actually represents a unique int type value. The which in Setpositivebutton, like the one above, represents the -3,setneutralbutton in -1,setnegativebutton represented by the-2. Here I believe you have thought of how concise writing, as long as write a response to the event, with the which parameter to distinguish is the button can be!


Look at the Concise Code 2: And the above effect is the same!

 private void Dialog1_1 () {///First new listener, set up monitor Dialoginterface.onclicklistener dialogoncliclistener=new Dialo                Ginterface.onclicklistener () {@Override public void OnClick (Dialoginterface dialog, int which) { Switch (which) {Case Dialog.BUTTON_POSITIVE:Toast.makeText (mainactiv                        Ity.this, "confirm" + which, Toast.length_short). Show ();                    Break Case Dialog.BUTTON_NEGATIVE:Toast.makeText (Mainactivity.this, "Cancel" + which, toast.length_short). Sh                        ow ();                    Break Case Dialog.BUTTON_NEUTRAL:Toast.makeText (mainactivity.this, "ignore" + which, toast.length_short). Sho                        W ();                Break        }            }        };  Dialog parameter Settings alertdialog.builder builder=new alertdialog.builder (this); First get the constructor builder.settitle ("hint"); Set Title Builder.setmessage("Are you sure to exit?"); Set content Builder.seticon (r.mipmap.ic_launcher);//set icon, image ID Builder.setpositivebutton ("Confirm", Dialogoncliclisten        ER);        Builder.setnegativebutton ("Cancel", Dialogoncliclistener);        Builder.setneutralbutton ("Ignore", dialogoncliclistener);    Builder.create (). Show (); }


List dialog box:


Code:

private void Dialog2 () {final String items[]={"Zhang San", "John Doe", "Harry"};  Dialog parameter Settings alertdialog.builder builder=new alertdialog.builder (this); First get the constructor builder.settitle ("hint"); Set the title//builder.setmessage ("Confirm exit?"); Set the content Builder.seticon (r.mipmap.ic_launcher);//Set the icon, the image ID can//settings list display, note that the list is set to not set Builder.setmessage (), otherwise the list        does not work. Builder.setitems (items,new Dialoginterface.onclicklistener () {@Override public void OnClick (Dialogi                Nterface dialog, int which) {Dialog.dismiss ();            Toast.maketext (Mainactivity.this, Items[which], Toast.length_short). Show ();        }        }); Builder.setpositivebutton ("OK", new Dialoginterface.onclicklistener () {@Override public void OnClick (dialoginterface dialog, int which)                {Dialog.dismiss ();            Toast.maketext (mainactivity.this, "OK", Toast.length_short). Show ();        }        }); BuildeR.create (). Show (); }


Description: The list dialog box simply sets the items property, noting that the message property cannot be set, otherwise only the message is displayed and the list is not displayed. The which attribute in the onclick in Setitems here is the subscript for the items array!

radio Button dialog box:


Code:

private void Dialog3 () {        final String items[]={"male", "female"};        Alertdialog.builder builder=new Alertdialog.builder (this);  First get the constructor        builder.settitle ("hint");//Set title        Builder.seticon (R.mipmap.ic_launcher);//set icon, image ID        Builder.setsinglechoiceitems (items,0,new Dialoginterface.onclicklistener () {            @Override public            void OnClick (dialoginterface dialog, int which) {                //dialog.dismiss ();                Toast.maketext (Mainactivity.this, Items[which], Toast.length_short). Show ();            }        );        Builder.setpositivebutton ("OK", new Dialoginterface.onclicklistener () {            @Override public            void OnClick ( Dialoginterface dialog, int which) {                Dialog.dismiss ();                Toast.maketext (mainactivity.this, "OK", Toast.length_short). Show ();            }        );        Builder.create (). Show ();    

Description: In fact, there is nothing to say, and the above, just set a single selection of setsinglechoiceitems, note the parameters here: items are displayed text, 0 means the default check is the first, is the default check "male."

Multi-select list dialog box:

Code:

private void Dialog4 () {final String items[]={"basketball", "soccer", "volleyball"};        Final Boolean selected[]={true,false,true};  Alertdialog.builder builder=new Alertdialog.builder (this); First get the constructor builder.settitle ("hint"); Set the title Builder.seticon (r.mipmap.ic_launcher);//set icon, image ID Builder.setmultichoiceitems (items,selected,new Di Aloginterface.onmultichoiceclicklistener () {@Override public void OnClick (Dialoginterface dialog, I                NT which, Boolean isChecked) {//Dialog.dismiss ();            Toast.maketext (Mainactivity.this, items[which]+ischecked, Toast.length_short). Show ();        }        }); Builder.setpositivebutton ("OK", new Dialoginterface.onclicklistener () {@Override public void OnClick (dialoginterface dialog, int which)                {Dialog.dismiss ();                Toast.maketext (mainactivity.this, "OK", Toast.length_short). Show ();      Android will automatically change the value of the selected array based on your choice.          for (int i=0;i<selected.length;i++) {log.e ("Hongliang", "" "+selected[i]);        }            }        });    Builder.create (). Show (); }


Description: The parameter in Setmultichoiceitems: Selected is the default corresponding selected state. When you choose, the system will automatically help you to change the value in the selected, so you can get all the selection status in the OK button. The others are the same as the radio.

Summary: In order to better use the dialog box, the custom dialog box is indispensable, of course, to communicate with the activity, callback is also indispensable, will be described in detail later. Not much to say, the complete code to a copy:

Mainactivity.java

Package Com.example.liang.dialogdemo;import Android.app.alertdialog;import Android.app.dialog;import Android.content.dialoginterface;import Android.os.bundle;import Android.support.v7.app.actionbaractivity;import Android.util.log;import Android.view.view;import Android.widget.button;import Android.widget.Toast;public class    Mainactivity extends Actionbaractivity implements view.onclicklistener{private Button btn1;    Private Button btn2;    Private Button btn3;    Private Button Btn4;        @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.activity_main);        btn1= (Button) Findviewbyid (R.ID.MAIN_BTN1);        Btn1.setonclicklistener (this);        Btn2= (Button) Findviewbyid (R.ID.MAIN_BTN2);        Btn2.setonclicklistener (this);        btn3= (Button) Findviewbyid (R.ID.MAIN_BTN3);        Btn3.setonclicklistener (this);        btn4= (Button) Findviewbyid (R.ID.MAIN_BTN4); Btn4.setonclickListener (this);  } private void Dialog1 () {Alertdialog.builder builder=new alertdialog.builder (this); First get the constructor builder.settitle ("hint"); Set the title Builder.setmessage ("Confirm exit?"); Set content Builder.seticon (r.mipmap.ic_launcher);//set icon, image ID can be Builder.setpositivebutton ("OK", new Dialoginterfa Ce.                Onclicklistener () {//Set OK button @Override public void OnClick (Dialoginterface dialog, int which) { Dialog.dismiss ();            Close dialog Toast.maketext (mainactivity.this, "confirm" + which, Toast.length_short). Show ();        }        }); Builder.setnegativebutton ("Cancel", new Dialoginterface.onclicklistener () {//Set Cancel button @Override public vo                ID OnClick (dialoginterface dialog, int which) {Dialog.dismiss ();            Toast.maketext (Mainactivity.this, "Cancel" + which, Toast.length_short). Show ();        }        }); Builder.setneutralbutton ("Ignore", new Dialoginterface.onclicklistener() {//Set ignore button @Override public void OnClick (Dialoginterface dialog, int which) {Dialo                G.dismiss ();            Toast.maketext (mainactivity.this, "ignore" + which, Toast.length_short). Show ();        }        });    The parameters are set to complete, created and displayed Builder.create (). Show (); } private void Dialog1_1 () {///First new listener, set up monitor Dialoginterface.onclicklistener dialogoncliclistener=new D                 Ialoginterface.onclicklistener () {@Override public void OnClick (Dialoginterface dialog, int which) { Switch (which) {Case Dialog.BUTTON_POSITIVE:Toast.makeText (MainA                        Ctivity.this, "confirm" + which, Toast.length_short). Show ();                    Break Case Dialog.BUTTON_NEGATIVE:Toast.makeText (Mainactivity.this, "Cancel" + which, toast.length_short). Sh                        ow ();                    Break              Case Dialog.button_neutral:          Toast.maketext (mainactivity.this, "ignore" + which, Toast.length_short). Show ();                Break        }            }        };  Dialog parameter Settings alertdialog.builder builder=new alertdialog.builder (this); First get the constructor builder.settitle ("hint"); Set the title Builder.setmessage ("Confirm exit?"); Set content Builder.seticon (r.mipmap.ic_launcher);//set icon, image ID Builder.setpositivebutton ("Confirm", Dialogoncliclisten        ER);        Builder.setnegativebutton ("Cancel", Dialogoncliclistener);        Builder.setneutralbutton ("Ignore", dialogoncliclistener);    Builder.create (). Show ();        } private void Dialog2 () {final String items[]={"Zhang San", "John Doe", "Harry"};  Dialog parameter Settings alertdialog.builder builder=new alertdialog.builder (this); First get the constructor builder.settitle ("hint"); Set the title//builder.setmessage ("Confirm exit?"); Set the content Builder.seticon (r.mipmap.ic_launcher);//Set the icon, the image ID can//settings list display, note that the list is set to not set Builder.setmessage (), otherwise the list        does not work. BuIlder.setitems (items,new Dialoginterface.onclicklistener () {@Override public void OnClick (dialogint                Erface dialog, int which) {Dialog.dismiss ();            Toast.maketext (Mainactivity.this, Items[which], Toast.length_short). Show ();        }        }); Builder.setpositivebutton ("OK", new Dialoginterface.onclicklistener () {@Override public void OnClick (dialoginterface dialog, int which)                {Dialog.dismiss ();            Toast.maketext (mainactivity.this, "OK", Toast.length_short). Show ();        }        });    Builder.create (). Show ();        } private void Dialog3 () {final String items[]={"male", "female"};  Alertdialog.builder builder=new Alertdialog.builder (this); First get the constructor builder.settitle ("hint"); Set the title Builder.seticon (r.mipmap.ic_launcher);//set icon, image ID can be builder.setsinglechoiceitems (items,0,new dialogin Terface. Onclicklistener () {@Override PubLIC void OnClick (dialoginterface dialog, int which) {//dialog.dismiss ();            Toast.maketext (Mainactivity.this, Items[which], Toast.length_short). Show ();        }        }); Builder.setpositivebutton ("OK", new Dialoginterface.onclicklistener () {@Override public void OnClick (dialoginterface dialog, int which)                {Dialog.dismiss ();            Toast.maketext (mainactivity.this, "OK", Toast.length_short). Show ();        }        });    Builder.create (). Show ();        } private void Dialog4 () {final String items[]={"basketball", "soccer", "volleyball"};        Final Boolean selected[]={true,false,true};  Alertdialog.builder builder=new Alertdialog.builder (this); First get the constructor builder.settitle ("hint"); Set the title Builder.seticon (r.mipmap.ic_launcher);//set icon, image ID Builder.setmultichoiceitems (items,selected,new Di Aloginterface.onmultichoiceclicklistener () {@Override public void OnClick (DiAloginterface dialog, int which, Boolean isChecked) {//Dialog.dismiss ();            Toast.maketext (Mainactivity.this, items[which]+ischecked, Toast.length_short). Show ();        }        }); Builder.setpositivebutton ("OK", new Dialoginterface.onclicklistener () {@Override public void OnClick (dialoginterface dialog, int which)                {Dialog.dismiss ();                Toast.maketext (mainactivity.this, "OK", Toast.length_short). Show ();                Android will automatically change the value of the selected array based on your choice.                for (int i=0;i<selected.length;i++) {log.e ("Hongliang", "" "+selected[i]);        }            }        });    Builder.create (). Show (); } @Override public void OnClick (View v) {switch (V.getid ()) {r.id.main_btn1:/                /Dialog1 ();            Dialog1_1 ();//equivalent to the above Dialog1 (), more concise than the proposed break; Case R.ID.MAIN_BTN2:DIALOG2 ();               Break                Case R.id.main_btn3:dialog3 ();            Break                Case R.id.main_btn4:dialog4 ();        Break }    }}

Layout file:

<linearlayout 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:orien tation= "vertical" android:paddingbottom= "@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/main_btn1 "android:layout_width=" Wrap_content "Andro        id:layout_height= "Wrap_content" android:text= "Normal Dialog"/> <button android:id= "@+id/main_btn2" Android:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:text= "List Dialog"/> <B Utton android:id= "@+id/main_btn3" android:layout_width= "wrap_content" android:layout_height= "Wrap_con Tent "Android: text= "Radio List dialog box"/> <button android:id= "@+id/main_btn4" android:layout_width= "Wrap_content" a ndroid:layout_height= "Wrap_content" android:text= "Multi-select list dialog"/></linearlayout>



Android's Dialog detailed

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: 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.