Layout file:
1 <?XML version= "1.0" encoding= "Utf-8"?>2 <LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"3 Xmlns:tools= "Http://schemas.android.com/tools"4 Android:layout_width= "Match_parent"5 Android:layout_height= "Match_parent"6 Android:paddingbottom= "@dimen/activity_vertical_margin"7 Android:paddingleft= "@dimen/activity_horizontal_margin"8 Android:paddingright= "@dimen/activity_horizontal_margin"9 Android:paddingtop= "@dimen/activity_vertical_margin"Ten Tools:context= "Com.hanqi.testapp2.TestActivity5" One android:orientation= "vertical"> A - <Button - Android:layout_width= "Match_parent" the Android:layout_height= "Wrap_content" - Android:text= "General dialog box" - Android:onclick= "Bt1_onclick"/> - <Button + Android:layout_width= "Match_parent" - Android:layout_height= "Wrap_content" + Android:onclick= "Bt2_onclick" A Android:text= "Radio dialog box"/> at <Button - Android:layout_width= "Match_parent" - Android:layout_height= "Wrap_content" - Android:onclick= "Bt3_onclick" - Android:text= "Check dialog box"/> - </LinearLayout>
Java Class Code:
1 Package com.hanqi.testapp2;2 3 import Android.app.AlertDialog;4 import Android.content.DialogInterface;5 import android.support.v7.app.AppCompatActivity;6 import Android.os.Bundle;7 import Android.view.View;8 import Android.widget.Toast;9 Ten Public class TestActivity5 extends Appcompatactivity { One A @Override - protected void OnCreate (Bundle savedinstancestate) { - super.oncreate (savedinstancestate); the Setcontentview (R.LAYOUT.ACTIVITY_TEST5); - } - //General dialog box - Public void Bt1_onclick (View v) + { - //dialog box cannot be instantiated directly + //The constructor is provided internally A //Method chain Call at Alertdialog alertdialog = new Alertdialog.builder (this) - . Settitle ("confirmation dialog box") - . Setmessage ("Sure to delete") - . Setpositivebutton ("Confirm", new Dialoginterface.onclicklistener () { - @Override - Public void OnClick (dialoginterface dialog, int which) { in Toast.maketext (testactivity5.this, "perform delete, which =" + which, Toast.length_short). Show (); - } to })//Front button + . Setnegativebutton ("Cancel", new Dialoginterface.onclicklistener () { - @Override the Public void OnClick (dialoginterface dialog, int which) { * Toast.maketext (Testactivity5.this, "undelete, which =" + which, Toast.length_short). Show (); $ }Panax Notoginseng }) - . Setneutralbutton ("neutral", new Dialoginterface.onclicklistener () { the @Override + Public void OnClick (dialoginterface dialog, int which) { A Toast.maketext (testactivity5.this, "button, which =" + which, Toast.length_short). Show (); the } + }). Setcancelable (False) - . Show ();//Display dialog box $ } $ //Radio dialog box - Public void Bt2_onclick (View v) - { the //final can extend the life cycle of a constant to the entire instance - final String [] str = {"Male", "female"};Wuyi New Alertdialog.builder (this) the . Settitle ("Radio dialog box") - . Setsinglechoiceitems (str, 0, new Dialoginterface.onclicklistener () { Wu @Override - Public void OnClick (dialoginterface dialog, int which) { About Toast.maketext (testactivity5.this, "which =" +which+ $ ", select" +str[which ", Toast.length_short). Show (); - //Close dialog box - Dialog.dismiss (); - } A }). Setcancelable (False) + . Show (); the } - //Check dialog box $ Public void Bt3_onclick (View v) the { the final String [] str = {"BMW", "Mercedes", "Rolls-Royce", "Bentley"}; the final boolean[] ch = {True,false,false,true}; the New Alertdialog.builder (this) - . Settitle ("Check dialog box") in . Setmultichoiceitems (str, CH, new Dialoginterface.onmultichoiceclicklistener () { the @Override the Public void OnClick (dialoginterface dialog, int which, Boolean isChecked) { About //Change the selected state of the corresponding array item the Ch[which] = isChecked; the } the }) + . Setpositivebutton ("Confirm", new Dialoginterface.onclicklistener () { - @Override the Public void OnClick (dialoginterface dialog, int which) {Bayi int i = 0; the //Get the final check state the For (Boolean b:ch) - { - Toast.maketext (testactivity5.this, str[i]+ "checked state =" + the (b? " Check ":" Unchecked), Toast.length_short). Show (); the i++; the } the } - }) the . Setnegativebutton ("Cancel", null) the . Setcancelable (False) the . Show ();94 } the}
:
android-dialog box