Android's Alertdialog

Source: Internet
Author: User

Transferred from: http://www.2cto.com/kf/201205/131876.html

Reference article: http://blog.csdn.net/flyfight88/article/details/8602162

Layout file

    <linearlayout android:layout_width= "match_parent" android:layout_height= "Match_parent" Android : orientation= "Vertical" > <button android:layout_width= "match_parent" Android:layout_hei ght= "Wrap_content" android:id= "@+id/withbutton" android:onclick= "DoClick" android:text= "with dialog box for multiple buttons "/> <button android:layout_width=" match_parent "android:layout_height=" Wrap_co        Ntent "android:id=" @+id/list "android:onclick=" DoClick "android:text=" List dialog box/> <button android:layout_width= "match_parent" android:layout_height= "Wrap_content" Android             Oid:id= "@+id/radiolist" android:onclick= "DoClick" android:text= "List dialog with radio button"/> <button Android:layout_width= "Match_parent" android:layout_height= "wrap_content" android:id= "@+i         D/checkboxlist "   android:onclick= "DoClick" android:text= "List dialog with multi-select button"/> <button android:layout_width = "Match_parent" android:layout_height= "wrap_content" android:id= "@+id/my" Android:onclick = "DoClick" android:text= "Custom"/> </LinearLayout>

public class Mainactivity extends Appcompatactivity {private Alertdialog.builder Builder;        @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);    Setcontentview (R.layout.activity_main);                } public void DoClick (View v) {switch (V.getid ()) {case R.id.withbutton: {                Builder=new Alertdialog.builder (this);                Builder.settitle ("title");                Builder.setmessage ("message");                Builder.seticon (R.mipmap.ic_launcher); Builder.setpositivebutton ("OK", new Dialoginterface.onclicklistener () {@Override PU Blic void OnClick (dialoginterface dialog, int which) {Toast.maketext (mainactivity.this, "OK delete", to Ast.                    Length_short). Show ();                }                }); Builder.setnegativebutton ("Cancel", new Dialoginterface.onclicklistener () {@Override                   public void OnClick (Dialoginterface dialog, int which) {Toast.maketext (mainacti                    Vity.this, "undelete", Toast.length_short). Show ();                }                }); Builder.setneutralbutton ("Ignore", new Dialoginterface.onclicklistener () {@Override Pub LIC void OnClick (dialoginterface dialog, int which) {Toast.maketext (mainactivity.this, "Ignore", Toast .                    Length_short). Show ();                }                });                Builder.create (). Show ();            Break  } case r.id.list: {final string[] arrayfruit = new string[] {"Apple", "orange", "Strawberry", "banana"                };                        Builder=new Alertdialog.builder (this). Settitle ("What kind of fruit do you like to eat?")                        ").                        SetIcon (R.mipmap.ic_launcher). Setitems (Arrayfruit, New Dialoginterface.onclicklistener () {@OverRide public void OnClick (Dialoginterface dialog, int. which) {to                            Ast.maketext (Mainactivity.this, Arrayfruit[which], Toast.length_short). Show ();                        }                        }).                            Setnegativebutton ("Cancel", new Dialoginterface.onclicklistener () {@Override public void OnClick (Dialoginterface dialog, int which) {//TODO auto-generated Metho                D stub}});                Builder.create (). Show ();            Break  } case r.id.radiolist: {final string[] arrayfruit = new string[] {"Apple", "orange", "Strawberry",                "Banana"};                        Builder=new Alertdialog.builder (this). Settitle ("What kind of fruit do you like to eat?")                        ").                        SetIcon (R.mipmap.ic_launcher). Setsinglechoiceitems (Arrayfruit, 0, New Dialoginterface.onclicklistener () {@Override public void on Click (dialoginterface dialog, int which) {Toast.maketext (Mainactivity.this, Arrayfruit[whi                            CH], toast.length_short). Show ();                }                        });                Builder.create (). Show ();            Break                } case r.id.checkboxlist: {final string[] arrayfruit = new string[] {"Apple", "orange", "Strawberry", "banana"};                Final boolean[] arrayfruitselected = new boolean[] {True, True, False, false};                        Builder=new Alertdialog.builder (this). Settitle ("What kind of fruit do you like to eat?")                        ").                        SetIcon (R.mipmap.ic_launcher).                            Setmultichoiceitems (Arrayfruit, arrayfruitselected, New Dialoginterface.onmultichoiceclicklistener () { @Override public void OnClick (Dialoginterface DIalog, int which, Boolean isChecked) {Arrayfruitselected[which] = isChecked;                }                        });                            Builder.setpositivebutton ("Confirm", new Dialoginterface.onclicklistener () {@Override public void OnClick (Dialoginterface dialog, int which) {StringBuilder string                                Builder = new StringBuilder (); for (int i = 0; i < arrayfruitselected.length; i++) {if (arrayfruitselected[i] = = T Rue) {Stringbuilder.append (Arrayfruit[i] + ",")                                    ; }} String str=stringbuilder.tostring (). substring (0,stringbu                                Ilder.tostring (). Length ()-1). ToString (); Toast.maketext (Mainactivity.this, str,Toast.length_short). Show ();                }}). Create ();                Builder.show ();            Break }        }    }}

  

 

The following is a custom dialog box

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "android:orientation=" horizontal "android:layout_width=" Match_parent "android:layout_height=" match_parent "> <linearlayout android:layout_width=" wrap_content "android:layout_height=" 100DP "Android:o rientation= "vertical" > <textview android:layout_width= "wrap_content" android:layout_he  ight= "50DP" android:textsize= "20sp" android:gravity= "center_vertical" android:text= "User name:"            /> <textview android:layout_width= "wrap_content" android:layout_height= "50DP" Android:textsize= "20SP" android:gravity= "center_vertical" android:text= "Password:"/> </lin earlayout> <linearlayout android:layout_width= "wrap_content" android:layout_height= "100DP" a ndroid:orientation= "Vertical" >       <edittext android:layout_width= "match_parent" android:layout_height= "50DP" Android            Oid:textsize= "20SP" android:hint= "Mailbox"/> <edittext android:layout_width= "Match_parent" android:layout_height= "50DP" android:textsize= "20sp" android:hint= "Your password must be kept secret yo"/> &lt ;/linearlayout></linearlayout>

public class Mainactivity extends Appcompatactivity {private Alertdialog.builder Builder;    Private Layoutinflater Inflater;        @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);    Setcontentview (R.layout.activity_main); } public void DoClick (View v) {switch (V.getid ()) {case r.id.my: {//Get Custom                View Inflater = Layoutinflater.from (this);                View MyLoginView = inflater.inflate (r.layout.mylayout, NULL);                        Builder = new Alertdialog.builder (this).                        Settitle ("User Login").                        SetIcon (R.mipmap.ic_launcher).                Setview (MyLoginView);                            Builder.setpositivebutton ("Login", new Dialoginterface.onclicklistener () {@Override public void OnClick (Dialoginterface dialog, int which) {//TODO Auto-generated method Stub}}).                            Setnegativebutton ("Cancel", new Dialoginterface.onclicklistener () {@Override public void OnClick (Dialoginterface dialog, int which) {//TODO auto-generated Metho                        D stub}}).                Create ();                Builder.show ();            Break }        }    }}

Show

Android's Alertdialog

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.