8 Kinds of Android dialog box (Dialog) using the method detailed _android

Source: Internet
Author: User
Tags gettext int size

This article summarizes the Android 8 kinds of dialog box (Dialog) Use method, share for everyone to reference, specific content as follows

1. Written in front

Android provides a rich dialog function, this article describes the use of the 8 most commonly used dialog boxes, including common (include hint messages and buttons), lists, radio, multiple selection, wait, progress bar, editing, customization and many other forms, will be introduced in the 2nd part.
Sometimes we want to do some specific function when the dialog box is created or closed, which requires a replication dialog create (), show (), dismiss (), and so on, which is described in part 3rd.

2. code example

2.1 General dialog (Figure 1 and Figure 2)

2 buttons

public class Mainactivity extends activity {@Override protected void onCreate (Bundle savedinstancestate) {sup
    Er.oncreate (savedinstancestate);
    Setcontentview (R.layout.activity_main);
    Button Buttonnormal = (button) Findviewbyid (r.id.button_normal); Buttonnormal.setonclicklistener (New View.onclicklistener () {@Override public void OnClick (View v) {s
      Hownormaldialog ();
  }
    });
     private void Shownormaldialog () {/* @setIcon Setting dialog box icon * @setTitle Setting dialog box title * @setMessage Setting dialog box message prompt * The Setxxx method returns the dialog object, so it can be chained to set the property/final Alertdialog.builder Normaldialog = new Alertdialog.builder (Main
    Activity.this);
    Normaldialog.seticon (R.drawable.icon_dialog);
    Normaldialog.settitle ("I'm an Ordinary dialog") normaldialog.setmessage ("which button do you want to click?"); Normaldialog.setpositivebutton (OK), new Dialoginterface.onclicklistener () {@Override public void Oncl
      Ick (dialoginterface dialog, int which) {  //...
    To-do}}); Normaldialog.setnegativebutton ("Off", new Dialoginterface.onclicklistener () {@Override public void Oncl Ick (dialoginterface dialog, int which) {//...
    To-do}});
  Display Normaldialog.show ();
 }
}

3 buttons

/* @setNeutralButton Set the middle button
 * If you need only one button, only set Setpositivebutton can be * * *
private void Showmultibtndialog () {
  Alertdialog.builder normaldialog = 
    new Alertdialog.builder (mainactivity.this);
  Normaldialog.seticon (r.drawable.icon_dialog);
  Normaldialog.settitle ("I am an ordinary dialog"). Setmessage ("which button do you want to click?");
  Normaldialog.setpositivebutton ("button 1", 
    new Dialoginterface.onclicklistener () {
    @Override public
    Void OnClick (dialoginterface dialog, int which) {
      //... To-do
    }
  });
  Normaldialog.setneutralbutton ("button 2", 
    new Dialoginterface.onclicklistener () {
    @Override public
    Void OnClick (dialoginterface dialog, int which) {
      //... To-do
    }
  });
  Normaldialog.setnegativebutton ("button 3", new Dialoginterface.onclicklistener () {
    @Override public
    Void OnClick (dialoginterface dialog, int which) {
      //... To-do
    }
  });
  Create an instance and display
  normaldialog.show ();
}

2.2 List dialog (Figure 3)

private void Showlistdialog () {
  final string[] items = {"I am 1", "I am 2", "I am 3", "I am 4"};
  Alertdialog.builder Listdialog = 
    new Alertdialog.builder (mainactivity.this);
  Listdialog.settitle ("I am a list dialog");
  Listdialog.setitems (items, new Dialoginterface.onclicklistener () {
    @Override public
    void OnClick ( Dialoginterface dialog, int which) {
      //which subscript starting from 0
      //... To-do
      Toast.maketext (mainactivity.this, 
        "you clicked" + Items[which], 
        toast.length_short). Show ();
  listdialog.show ();
}

2.3 Single Selection dialog (Figure 4)

int yourchoice;
private void Showsinglechoicedialog () {
  final string[] items = {"I am 1", "I am 2", "I am 3", "I am 4"};
  Yourchoice =-1;
  Alertdialog.builder Singlechoicedialog = 
    new Alertdialog.builder (mainactivity.this);
  Singlechoicedialog.settitle ("I am a single election dialog");
  The second parameter is the default option, which is set to 0
  singlechoicedialog.setsinglechoiceitems (items, 0, 
    New Dialoginterface.onclicklistener () {
    @Override public
    void OnClick (dialoginterface dialog, int which) {
      yourchoice = which;
    }
  );
  Singlechoicedialog.setpositivebutton (ok 
    ), new Dialoginterface.onclicklistener () {
    @Override
    public void OnClick (Dialoginterface dialog, int which) {
      if (yourchoice!=-1) {
        Toast.maketext ( Mainactivity.this, 
        "You have selected" + Items[yourchoice], 
        toast.length_short). Show ();}}
  ;
  Singlechoicedialog.show ();
}

2.4 Multiple Selection dialog (Figure 5)

arraylist<integer> yourchoices = new arraylist<> ();
  private void Showmultichoicedialog () {final string[] items = {"I am 1", "I am 2", "I am 3", "I am 4"};
  Set the default selected option, all false default is not selected Final Boolean initchoicesets[]={false,false,false,false};
  Yourchoices.clear ();
  Alertdialog.builder Multichoicedialog = new Alertdialog.builder (mainactivity.this);
  Multichoicedialog.settitle ("I am a more selective dialog"); Multichoicedialog.setmultichoiceitems (items, initchoicesets, new Dialoginterface.onmultichoiceclicklistener () {@O
        Verride public void OnClick (Dialoginterface dialog, int which, Boolean ischecked) {if (ischecked) {
      Yourchoices.add (which);
      else {yourchoices.remove (which);
  }
    }
  }); Multichoicedialog.setpositivebutton (OK), new Dialoginterface.onclicklistener () {@Override public void oncli
      CK (dialoginterface dialog, int which) {int size = Yourchoices.size ();
      String str = ""; for (int i = 0; I &lT Size
      i++) {str + + items[yourchoices.get (i)] + "";
    Toast.maketext (Mainactivity.this, "You have selected" + str, toast.length_short). Show ();
  }
  });
Multichoicedialog.show ();
 }

2.5 Waiting for dialog (Figure 6)

private void Showwaitingdialog () {/
  * Wait dialog has interactivity to mask other controls
   * @setCancelable to make the screen not clickable, set to not cancel (false)
   * After the event is completed, the active call function closes the dialog
   * * *
  progressdialog waitingdialog= 
    new ProgressDialog (mainactivity.this);
  Waitingdialog.settitle ("I am a waiting dialog");
  Waitingdialog.setmessage ("Waiting ...");
  Waitingdialog.setindeterminate (true);
  Waitingdialog.setcancelable (false);
  Waitingdialog.show ();
}

2.6 Progress Bar dialog (Figure 7)

private void Showprogressdialog () {/
  * @setProgress Setting initial progress
   * @setProgressStyle setting Style (horizontal progress bar)
   * @setMax Set the maximum progress
  /FINAL int max_progress =;
  Final ProgressDialog ProgressDialog = 
    new ProgressDialog (mainactivity.this);
  Progressdialog.setprogress (0);
  Progressdialog.settitle ("I am a progress bar dialog");
  Progressdialog.setprogressstyle (progressdialog.style_horizontal);
  Progressdialog.setmax (max_progress);
  Progressdialog.show ();
  /* Simulate progress increase process
   * Open a new thread, each 100ms, progress increased by 1 * *
  (new Runnable () {
    @Override public
    void Run () {
      int progress= 0;
      while (Progress < max_progress) {
        try {
          thread.sleep ();
          progress++;
          Progressdialog.setprogress (progress);
        } catch (Interruptedexception e) {
          e.printstacktrace ();
        }
      }
      The window disappears
      progressdialog.cancel ();}) after the maximum progress is reached
  . Start ();
}

2.7 Edit dialog (Figure 8)

private void Showinputdialog () {/
  * @setView mount a editview
   * *
  final EditText edittext = new EditText ( Mainactivity.this);
  Alertdialog.builder Inputdialog = 
    new Alertdialog.builder (mainactivity.this);
  Inputdialog.settitle ("I am an input dialog"). Setview (edittext);
  Inputdialog.setpositivebutton (ok 
    ), new Dialoginterface.onclicklistener () {
    @Override public
    Void OnClick (dialoginterface dialog, int which) {
      toast.maketext (mainactivity.this,
      edittext.gettext (). ToString (), 
      toast.length_short). Show ();
    }
  }. Show ();
}

2.8 Custom Dialog (Figure 9)

<!--res/layout/dialog_customize.xml--> <!--custom view--> <linearlayout xmlns:android= "http:// Schemas.android.com/apk/res/android "android:orientation=" vertical "android:layout_width=" Match_parent "Android:
    layout_height= "Match_parent" > <edittext android:id= "@+id/edit_text" android:layout_width= "Match_parent" android:layout_height= "Wrap_content"/> </LinearLayout> private void Showcustomizedialog () {/* @setVie W Mount Custom View ==> r.layout.dialog_customize * Because Dialog_customize.xml only placed a editview, so and figure 81 * Dialog_customize.xml can be self
  Define more complex View */alertdialog.builder Customizedialog = new Alertdialog.builder (mainactivity.this);
  Final View Dialogview = Layoutinflater.from (mainactivity.this). Inflate (R.layout.dialog_customize,null);
  Customizedialog.settitle ("I am a custom dialog");
  Customizedialog.setview (Dialogview); Customizedialog.setpositivebutton (OK), new Dialoginterface.onclicklistener () {@Override PUBlic void OnClick (dialoginterface dialog, int which) {//Get input from EditView edittext = (edit
      Text) Dialogview.findviewbyid (R.id.edit_text);
    Toast.maketext (Mainactivity.this, Edit_text.gettext (). toString (), Toast.length_short). Show ();
  }
  });
Customizedialog.show ();

 }

3. Replication callback function

/* Replication Builder the Create and show functions, which enable you to implement the necessary settings before dialog display * such as initialization lists, default options, and so on * @create called on the first creation * @show call/private void show on each display
  Listdialog () {final string[] items = {"I am 1", "I am 2", "I am 3", "I am 4"}; Alertdialog.builder Listdialog = new Alertdialog.builder (mainactivity.this) {@Override public alertdial
      OG Create () {Items[0] = "I am No.1";
    return Super.create ();
      @Override public Alertdialog Show () {items[1] = "I am No.2";
    return Super.show ();
  }
  };
  Listdialog.settitle ("I am a list dialog"); Listdialog.setitems (items, new Dialoginterface.onclicklistener () {@Override public void OnClick (Dialoginterface D Ialog, int which) {//...
  To-do}}); /* @setOnDismissListener Call * @setOnCancelListener When dialog is destroyed dialog call/Listdialog.setondismisslistener when shutdown (new Dia Loginterface.ondismisslistener () {public void Ondismiss (Dialoginterface dialog) {Toast.maketext (getapplication 
   Context (), "dialog was destroyed",     Toast.length_short). Show ();
  }
  });
Listdialog.show ();
 }

The above is the entire content of this article, I hope to help you learn.

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.