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 ();}}