Android Various dialog box summary notes

Source: Internet
Author: User

OK Cancel dialog box (with icon)
(context, subject) New Alertdialog.builder (this, alertdialog.theme_device_default_light)    . Settitle ("title"). Setmessage (" Content "). SetIcon (R.drawable.ic_launcher)//Respond to click events    . Setpositivebutton (" OK ", new Onclicklistener () {        @Override Public        void OnClick (dialoginterface dialog, int which) {            //TODO auto-generated method stub            Toast.maketext ( Mainactivity.this, "OK", 0). Show ();        }    }). Setnegativebutton ("Cancel", null). Show ();//Don't forget Show
Effect


Simple single-selection dialog box
Final string[] strs=new string[]{"male", "female", "Don't Tell You"};new Alertdialog.builder (This, alertdialog.theme_device_default_ Light). Settitle ("title"). SetIcon (R.drawable.ic_launcher)//(string array, default selection, Response event). Setsinglechoiceitems (STRs, 2, new Onclicklistener () {//swich: array of options id@overridepublic void OnClick (dialoginterface dialog, int which) {//TODO Auto-generated method Stubtoast.maketext (Mainactivity.this, Strs[which], 0). Show ();}}). Show ();
Effect

Multi-Select dialog box
Final string[] STRs = new string[] {"Banana", "apple", "pear"};    Alertdialog.builder dialog = new Alertdialog.builder (this,            alertdialog.theme_device_default_light). SetTitle (" Title ")            . SetIcon (R.drawable.ic_launcher)            . Setmultichoiceitems (STRs, New Boolean[]{true,true,false}, new Onmultichoiceclicklistener () {                //which: array of selected items id//ischecked: The selected state of the selection @Override the public                void OnClick ( Dialoginterface dialog, int which, Boolean isChecked) {                    //TODO auto-generated method stub                    log.d ("Test", "which=" +which+ ": ischecked=" +ischecked);                }            );                    Dialog.create (). Show ();
Effect


Progress dialog box
ProgressDialog pd=new ProgressDialog (This);pd. Settitle ("title");pd. Setmessage ("Bring to ...");pd. Show ();


Progress Bar dialog box
Final ProgressDialog pd = new ProgressDialog (this);    There are only two themes    pd.setprogressstyle (progressdialog.style_horizontal);//Set Maximum progress value    Pd.setmax (+);    Pd.settitle ("title");    Pd.setmessage ("Please bring to ...");    Pd.show ();    New Thread () {public        void run () {for            (int i = 1; i < i++) {                try {Thread.Sleep]                } cat CH (interruptedexception e) {                    //TODO auto-generated catch block                    e.printstacktrace ();                }                Pd.setprogress (i);            }            Can be closed on any thread,            Pd.dismiss ();        };    }. Start ();
Effect
Customize dialog box
Final EditText et_text = new EditText (this), new Alertdialog.builder (this). Settitle ("Please enter")//Put in a TextView        . SetIcon (Android. R.drawable.ic_dialog_info). Setview (Et_text)        . Setpositivebutton ("OK", new Onclicklistener () {            @Override Public            void OnClick (dialoginterface dialog, int which) {                //TODO auto-generated method stub                Toast.maketext ( Mainactivity.this,                        Et_text.gettext (). toString (), 0). Show ();            }        }). Setnegativebutton ("Cancel", null). Show ();

Effect

Customizing the Layout dialog boxLayout File
<?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 "and roid:orientation= "vertical" > <textview android:layout_width= "match_parent" android:layout_height= " Wrap_content "android:text="??? 

Summary notes for various Android dialog boxes

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.