Android_03 _ dialog box
MainActivity. java
Package com. itheima. dialog; import android. OS. bundle; import android. app. activity; import android. app. alertDialog; import android. app. alertDialog. builder; import android. content. dialogInterface; import android. content. dialogInterface. onClickListener; import android. content. dialogInterface. onMultiChoiceClickListener; import android. view. menu; 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);} // confirmation dialog box public void click1 (View v) {AlertDialog. builder builder = new Builder (this); // sets the icon builder. setIcon (android. r. drawable. alert_dark_frame); // sets the title builder. setTitle. setMessage (Li zhiping, think clearly); // set the confirmation button builder. setPos ItiveButton (OK, new OnClickListener () {@ Overridepublic void onClick (DialogInterface dialog, int which) {Toast. makeText (MainActivity. this, thanks for using this software. Goodbye, 0 ). show () ;}}); // set the cancel button builder. setNegativeButton (cancel, new OnClickListener () {@ Overridepublic void onClick (DialogInterface dialog, int which) {Toast. makeText (MainActivity. this, if you do not know yourself, you will not succeed, 0 ). show () ;}}); // Method 1: // use the builder to generate a dialog box object // AlertDialog ad = builder. Create (); // ad. show (); // Method 2: // You can also directly use the show () of builder. // The same is true for the two dialogs. show () ;}// single-choice dialog box public void click2 (View v) {AlertDialog. builder builder = new Builder (this); builder. setTitle (select gender); final String [] items = new String [] {male, female}; // The second parameter in setSingleChoiceItems, //-1 indicates that "male" is selected by default. In this example, 0 indicates that "male" is selected, and 1 indicates that "female" builder is selected. setSingleChoiceItems (items,-1, new OnClickListener () {// which: subscript of the entry selected by the user // dialog: the dialog box that triggers this method @ Over Ridepublic void onClick (DialogInterface dialog, int which) {Toast. makeText (MainActivity. this: + items [which], 0 ). show (); // close the dialog box dialog. dismiss () ;}}); builder. show () ;}// multiple choice dialog box public void click3 (View v) {AlertDialog. builder builder = new Builder (this); builder. setTitle (Please select someone you think is handsome); final String [] items = new String [] {Kan Ge, Zhao }; // indicates the corresponding default value final boolean [] checkedItems = new boolean [] {true, true, fa Lse, false}; builder. setMultiChoiceItems (items, checkedItems, new OnMultiChoiceClickListener () {// which: subscripts of the entries clicked by the user // isChecked: whether the user selects the entry or cancels the entry @ Overridepublic void onClick (DialogInterface dialog, int which, boolean isChecked) {checkedItems [which] = isChecked ;}}); // set a confirmation button builder. setPositiveButton (OK, new OnClickListener () {@ Overridepublic void onClick (DialogInterface dialog, int which) {String text =; For (int I = 0; I <4; I ++) {text + = checkedItems [I]? Items [I] +,:;} Toast. makeText (MainActivity. this, text, 0 ). show (); dialog. dismiss () ;}}); builder. show ();}}