Android Common dialog box

Source: Internet
Author: User

The following code writes a few common dialog boxes that contain:

1. Confirm the Cancel dialog box

2. Single-selection dialog box

3. Multi-Select dialog box

4. Progress dialog box (when loaded)

5. Download Progress dialog box

package com.yuanlp.dialogs;import android.app.progressdialog;import  android.content.dialoginterface;import android.os.bundle;import android.support.v7.app.alertdialog; import android.support.v7.app.appcompatactivity;import android.view.view;import  android.widget.toast;public class mainactivity extends appcompatactivity {      @Override     protected void oncreate (bundle  savedinstancestate)  {        super.oncreate (savedInstanceState);         setcontentview (R.layout.activity_main);     }    /**     *  Create a OK Cancel dialog box       *  @param  view     */    public void  Click1 (View view) {        //Factory design mode, get dialogBox Factory         AlertDialog.Builder builder = new  Alertdialog.builder (this);         builder.settitle ("Warning");         builder.setmessage ("to practice this work, must first self-palace");         builder.setpositivebutton ("Yes",  new dialoginterface.onclicklistener ()  {              @Override              public void onclick (Dialoginterface dialog, int which)  {                 Toast.maketext (Mainactivity.this, "clicked Yes", Toast.length_short). Show ();             }        });         builder.setNegativebutton ("No",  new dialoginterface.onclicklistener ()  {              @Override              public void onclick (Dialoginterface dialog, int which)  {                 toast.maketext ( Mainactivity.this, "Not from the palace, unsuccessful", Toast.length_short). Show ();             }        });         //must display show, otherwise it will not show         builder.show ();     }    /**     *  Create a single-selection dialog box       *  @param  view     */    public void  Click2 (View view) {        //Factory design mode, get dialog box factory         final  Alertdialog.builder builder = new alertdialog.builder (This);         builder.settitle ("Please select name");         final string[ ] items=new string[]{"Male", "female", "Unknown"};         Builder.setsinglechoiceitems (Items, -1, new dialoginterface.onclicklistener ()  {              @Override              public void onclick (Dialoginterface dialog, int  which)  {                 toast.maketext (Mainactivity.this, "Selected gender is:" +items[which],toast.length_long). Show ();                 //Close dialog Box                  dialog.dismiss ();             }        });         //must display show, otherwise it will not show         builder.show ();     }    /**     *  Create a multi-select dialog box       *  @param  view     */    public void click3 ( View view) {        //Factory design mode, get dialog box factory          alertdialog.builder builder = new alertdialog.builder (This);         builder.settitle ("Warning");         final string[] items={"Apple", "banana", "pear", "peach"};        final boolean[] checkdes={false , true,true,true};        //parameter resolution: The first one is a collection of multiple options, the second is the default selection state for each option, and the third is the listener         builder.setmultichoiceitems (items, checkdes, new  dialoginterface.onmultichoiceclicklistener ()  {              @Override              Public void onclick (dialoginterface dialog, int which, boolean ischecked)  {                 Toast.maketext (Mainactivity.this,items[which]+ischecked,toast.length_short). Show ();             }        });         bUilder.setpositivebutton ("Yes",  new dialoginterface.onclicklistener ()  {              @Override              public void onclick (Dialoginterface dialog, int which)  {                 stringbuffer  sb=new stringbuffer ();                 for (int i=0;i<checkdes.length;i++) {                     if  (Checkdes[i]) {                          sb.append (items[i]+ " ");                     }                 }                 toast.maketext (Mainactivity.this, "the fruit that likes to eat is:" +sb.tostring (), Toast.length_short). Show ();             }         });         //must display show, otherwise it will not be displayed          builder.show ();    }    /**      *  Create a Progress dialog box      *  @param  view     */     public void click4 (View view) {         //Factory design mode, get dialog box factory &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;FINAL&NBSP;PROGRESSDIALOG&NBSP;PD  = new progressdiaLog (this);         pd.settitle ("Wait");         pd.setmessage ("desperately loading");         //must display show, otherwise it will not show         pd.show ();       new  Thread () {            @Override             public void run ()  {                try {                    thread.sleep (20000);                } catch  ( Interruptedexception e)  {                    E.printstacktrace ();                }               pd.dismiss ();   //Close dialog Box            }        }.start ();     }    /**     *   Create a dialog box for a specific progress bar      *  @param  view     */  &NBSP;&NBSP;&NBSP;PUBLIC&NBSP;VOID&NBSP;CLICK5 (View view) {         //Factory design mode, get dialog box factory         final progressdialog pd =  new progressdialog (this);         pd.settitle ("Please Wait");         pd.setmessage ("desperately loading");         pd.setprogressstyle(progressdialog.style_horizontal);         pd.setmax (;  )       //must display show, otherwise it will not show         pd.show ( );         new thread () {              @Override              public void run ()  {                 try {                     for  (int i=0;i<100;i++) {                          thread.sleep (;            )              pd.setprogress (i);                     }                 } catch  (Interruptedexception e)  {                     e.printstacktrace ();                 }                 pd.dismiss ();            }         }.start ();     }}


This article is from the "Yuangushi" blog, make sure to keep this source http://cm0425.blog.51cto.com/10819451/1940215

Android Common dialog box

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.