Summary of commonly used dialogs in Android

Source: Internet
Author: User

Summary of commonly used dialogs in Android

Record frequently used dialog boxes for future reference.

Layout File

     
          
              
                  
  
 

Code Implementation

Package com. wzw. dialog; import android. app. activity; import android. app. alertDialog; import android. app. alertDialog. builder; import android. app. progressDialog; import android. content. dialogInterface; import android. content. dialogInterface. onClickListener; import android. content. dialogInterface. onMultiChoiceClickListener; import android. OS. bundle; import android. view. view; import android. widget. toast; public class MainActivity extends Activity {@ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main);}/*** confirm cancel dialog box * @ param v */public void click1 (View v) {AlertDialog. builder builder = new Builder (this); builder. setTitle ("I am the title"); builder. setMessage ("I Am a prompt message"); builder. setPositiveButton ("OK", new OnClickListener () {@ Overridepublic void onClick (DialogInterface dialog, int which) {Toast. makeText (getApplicationContext (), "confirm to be clicked", 0 ). show () ;}}); builder. setNegativeButton ("cancel", new OnClickListener () {@ Overridepublic void onClick (DialogInterface dialog, int which) {// Toast. makeText (getApplicationContext (), "canceled", 0 ). show (); // do not write anything, the default is to close the dialog box}); builder. create (). show ();}/*** single-choice dialog box * @ param v */public void click2 (View v) {AlertDialog. builder builder = new Builder (this); builder. setTitle ("My title"); final String items [] = {"male", "female", "diaosi"}; builder. setSingleChoiceItems (items, 2, new OnClickListener () {@ Overridepublic void onClick (DialogInterface dialog, int which) {Toast. makeText (getApplicationContext (), "Gender is" + items [which], 0 ). show (); dialog. dismiss () ;}}); builder. create (). show ();}/*** multiple choice dialog box * @ param v */public void click3 (View v) {AlertDialog. builder builder = new Builder (this); builder. setTitle ("My title"); final String [] items = {"apple", "banana", "chestnut", "watermelon "}; final boolean [] result = new boolean [] {true, false, true, false}; builder. setMultiChoiceItems (items, result, new OnMultiChoiceClickListener () {@ Overridepublic void onClick (DialogInterface dialog, int which, boolean isChecked) {// TODO Auto-generated method stubToast. makeText (getApplicationContext (), items [which] + isChecked, 0 ). show (); result [which] = isChecked ;}}); builder. setPositiveButton ("Submit", new OnClickListener () {@ Overridepublic void onClick (DialogInterface diich, int which) {StringBuffer sb = new StringBuffer (); for (int I = 0; I <result. length; I ++) {if (result [I]) {sb. append (items [I] + "--") ;}} Toast. makeText (getApplicationContext (), "Your Choice" + sb. toString (), 0 ). show () ;}}); builder. create (). show ();} public void click4 (View v) {ProgressDialog pd = new ProgressDialog (this); pd. setTitle ("prompt message"); pd. setMessage ("loading data, please wait"); pd. show ();}}


Related Article

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.