Various ways to use dialog

Source: Internet
Author: User

Today I learned dialog components, and will dialog do a summary, the following is the summary of the content and demo

Alertdialog: The most versatile and most widely used dialog box
ProgressDialog: progress dialog box,
Datepickerdialog: Date selection dialog box
Timepickerdialog: Time Selection dialog box


Alertdialog provides four predefined dialog boxes
"Message with n buttons in the prompt dialog box
"List with n buttons with List dialog box
"With multiple single-selection list items, a dialog box with n buttons
"With more than one multi-select list item, a dialog box with n buttons
In addition, you can create a custom dialog box for the interface
Procedure for creating a dialog box:
1. Define a Alertdialog.builder Object
2.
Set icon
SetIcon (),
Set Title
Settitle (),
Setting display content
Setmessage ()
Set OK button
Setpositivebutton
Set the Cancel button
Setnegativebutton
Create a decorative button
Setneutralbutton (charsequence Text,dialoginterface.onclicklistener listener);
Create a Display dialog box
Create (). Show ()
Implement a variety of dialog box highlights. Include (List dialog, Radio List dialog, multi-Select dialog box, custom dialog) Click the corresponding button to come up with the corresponding dialog box.
Steps
1. Define the XML file and complete the button layout.
2. Rewrite the Oncreatedialog method in activity to return the dialog object. Call the ShowDialog method to display the dialog box.

Mainactivity.java

Package Lzl.edu.com.dialogdemos;import Android.app.activity;import Android.app.alertdialog;import Android.app.dialog;import Android.content.dialoginterface;import Android.graphics.color;import Android.os.Bundle; Import Android.view.layoutinflater;import Android.view.view;import Android.widget.button;import Android.widget.edittext;import Android.widget.tablelayout;import Android.widget.textview;import Android.widget.toast;public class Mainactivity extends Activity implements view.onclicklistener{public static final in    t listdialog = 1;    public static final int singledialog = 2;    public static final int multidialog = 3;    public static final int logindialog = 4;    TextView Mtextview;    Alertdialog.builder Builder;    Button Listdialog,singledialog,multidialog,logindialog;    EditText Medittext,username,password;        @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.activity_main); InchIt ();        } void Init () {Listdialog = (Button) Findviewbyid (R.id.listdialog);        Singledialog= (Button) Findviewbyid (R.id.singledialog);        Multidialog= (Button) Findviewbyid (R.id.multidialog);        Logindialog= (Button) Findviewbyid (R.id.logindialog);        Mtextview = (TextView) Findviewbyid (R.id.mtextview);        Medittext = (EditText) Findviewbyid (R.id.medit);        Listdialog.setonclicklistener (this);        Singledialog.setonclicklistener (this);        Multidialog.setonclicklistener (this);    Logindialog.setonclicklistener (this);                } @Override public void OnClick (View v) {switch (V.getid ()) {r.id.listdialog:                ShowDialog (Listdialog);            Break                Case R.id.singledialog:showdialog (Singledialog);            Break                Case R.id.multidialog:showdialog (Multidialog);            Break Case R.id.logindialog:showdialog (Logindialog);               Break        Default:break;                }} @Override protected Dialog oncreatedialog (int id) {switch (ID) {Case listdialog:            return Onlistdialog ();            Case Singledialog:return Onsingledialog ();            Case Multidialog:return Onmutidialog ();            Case Logindialog:return Onlogindialog ();        Default:break;    } return null;        } Dialog Onlistdialog () {builder = new Alertdialog.builder (this);       Builder.settitle ("English level Six query");        Final string[] Contents =new string[]{"99 Quarters", "China Education Department", "Ministry of Educational Examination Center"}; Builder.setitems (contents, new Dialoginterface.onclicklistener () {@Override public void OnClick (Dia Loginterface dialog, int which) {switch (which) {case 0:MTEXTV                        Iew.setbackgroundcolor (color.red);     Mtextview.settext (Contents[which]);                   Break                        Case 1:mtextview.setbackgroundcolor (Color.green);                        Mtextview.settext (Contents[which]);                    Break                        Case 2:mtextview.setbackgroundcolor (Color.yellow);                        Mtextview.settext (Contents[which]);                    Break                Default:break;        }            }        });        Builder.setpositivebutton ("OK", null);    return Builder.create ();       } Dialog Onsingledialog () {builder = new Alertdialog.builder (this);        Final string[] Contents =new string[]{"99 Quarters", "China Education Department", "Ministry of Educational Examination Center"};        Builder.settitle ("English level Six query"); Builder.setsinglechoiceitems (contents, 1, New Dialoginterface.onclicklistener () {@Override public V          OID OnClick (dialoginterface dialog, int which) {switch (which) {case 0:              Mtextview.setbackgroundcolor (color.red);                        Mtextview.settext (Contents[which]);                    Break                        Case 1:mtextview.setbackgroundcolor (Color.green);                        Mtextview.settext (Contents[which]);                    Break                        Case 2:mtextview.setbackgroundcolor (Color.yellow);                        Mtextview.settext (Contents[which]);                    Break                Default:break;        }            }        });        Builder.setpositivebutton ("OK", null);    return Builder.create ();        } Dialog Onmutidialog () {builder = new Alertdialog.builder (this);        Builder.settitle ("Valentine's Day Gift");        Final string[] Qixigift = new string[]{"Couple movie tickets", "Couples package", "Couples bracelet", "Couple rings"};        Final boolean[] checkstates = new Boolean[]{false,false,false,false}; Builder.setmultichoiceitems (Qixigift, CheCkstates, New Dialoginterface.onmultichoiceclicklistener () {@Override public void OnClick (dialogint                Erface dialog, int which, Boolean isChecked) {String result = "The Tanabata gift you selected is:"; for (int i = 0; i < checkstates.length; i++) {if (Checkstates[i]) {result +                    = Qixigift[i];            }} medittext.settext (Result);        }        });    return Builder.create ();        } Dialog Onlogindialog () {builder = new Alertdialog.builder (this);        Loads the login layout file into the interface layoutinflater inflator = Layoutinflater.from (this);        Tablelayout loginform = (tablelayout) inflator.inflate (r.layout.logindialog,null);        UserName = (EditText) Loginform.findviewbyid (r.id.username);        Password = (EditText) Loginform.findviewbyid (R.id.password);        Builder.setview (LoginForm); Builder.setpositivebutton ("Login", new Dialoginterface.onclicklistener () {            @Override public void OnClick (Dialoginterface dialog, int. which) {String name = use                Rname.gettext (). toString ();            Toast.maketext (mainactivity.this, name + "ready to sign in", Toast.length_long). Show ();        }        }); Builder.setnegativebutton ("Cancel", new Dialoginterface.onclicklistener () {@Override public void onclic            K (dialoginterface Dialog, int which) {Toast.maketext (mainactivity.this, "Cancel Login", Toast.length_long). Show ();        }        });    return Builder.create (); }}
Logindialog.xml

<?xml version= "1.0" encoding= "Utf-8"? ><tablelayout android:id= "@+id/login_layout" xmlns:android= "http// Schemas.android.com/apk/res/android "android:orientation=" vertical "android:layout_width=" Match_parent "Android: layout_height= "Match_parent" ><textview android:layout_width= "fill_parent" android:layout_height= "Wrap_ Content "android:text=" User Login "/> <tablerow android:orientation=" Horizontal "Android:layout_wid Th= "Wrap_content" android:layout_height= "wrap_content" > <textview android:text= "username "Android:layout_width=" wrap_content "android:layout_height=" Wrap_content "/> & Lt EditText android:id= "@+id/username" android:hint= "Please enter user name" Android:layout_width= "Fill_pare NT "android:layout_height=" Wrap_content "/> </TableRow> <tablerow android:o rientation= "Horizontal" AndroId:layout_width= "Wrap_content" android:layout_height= "Wrap_content" > <textview Android oid:text= "Password" android:layout_width= "wrap_content" android:layout_height= "Wrap_content"/&        Gt <edittext android:hint= "Please enter password" android:id= "@+id/password" android:inputtype= "TEXTPASSW Ord "android:layout_width=" Fill_parent "android:layout_height=" wrap_content "/> &lt ;/tablerow></tablelayout>
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: paddingleft= "@dimen/activity_horizontal_margin" android:paddingright= "@dimen/activity_horizontal_margin" Android :p addingtop= "@dimen/activity_vertical_margin" android:paddingbottom= "@dimen/activity_vertical_margin" tools: Context= ".        Mainactivity "> <linearlayout android:orientation=" vertical "android:layout_width=" fill_parent " android:layout_height= "Fill_parent" ><button android:id= "@+id/listdialog" android:layout_width= "wrap _content "android:layout_height=" wrap_content "android:text=" List dialog box "android:layout_alignparenttop=" true "Andro Id:layout_alignparentstart= "true"/> <button android:id= "@+id/singledialog" Android:layout_width= "W     Rap_content "android:layout_height=" Wrap_content "   android:text= "Radio dialog box" android:layout_below= "@+id/logindialog" android:layout_alignparentstart= "true"/> <button android:id= "@+id/multidialog" android:layout_width= "Wrap_content" Android:layout_heigh t= "Wrap_content" android:text= "Multi-Select Dialog" android:layout_below= "@+id/singledialog" Android:layout_tostart        of= "@+id/singledialog" android:layout_margintop= "54DP"/> <button android:id= "@+id/logindialog" Android:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:text= "Login Dialog" and roid:layout_below= "@+id/listdialog" android:layout_alignparentstart= "true"/> <textview and            Roid:id= "@+id/mtextview" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" Android:textsize= "30sp"/> <edittext android:id= "@+id/medit" Android:layout_wid       Th= "Wrap_content"     android:layout_height= "Fill_parent" android:textsize= "30sp"/> </linearlayout></relativelayo Ut>




Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Various ways to use dialog

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.