(1) First we write a simple Alertdialog dialog box, to create a alertdialog, you need to use the Create () method in Alertdialog.builder, and then create a dialog box to set the properties of the dialog box, such as setting the title, icon, Content and so on.
Modify the next Mainactivity.java method (under Android Studio Tools ):
1 PackageCom.example.administrator.dialog1;2 3 Importandroid.app.Activity;4 ImportAndroid.os.Bundle;5 ImportAndroid.app.AlertDialog.Builder;6 ImportAndroid.app.AlertDialog;7 8 Public classMainactivityextendsActivity {9 Ten @Override One protected voidonCreate (Bundle savedinstancestate) { A Super. OnCreate (savedinstancestate); - Setcontentview (r.layout.activity_main); - the //to create a alertdialog, use the Create () method in the Alertdialog.builder -Builder Alertdialog =NewAlertdialog.builder ( This); -Alertdialog.settitle ("Simple dialog box");//Set Title -Alertdialog.setmessage ("Once Upon a man, he was cold, and at last he died of cold." ");//Set Content +Alertdialog.seticon (R.mipmap.ic_launcher);//Set icon - alertdialog.create (); + alertdialog.show (); A } at - -}
Operating effect:
(2) Next we will set the Alertdialog with the button, the code is as follows:
1 PackageCom.example.administrator.dialog1;2 3 Importandroid.app.Activity;4 ImportAndroid.os.Bundle;5 ImportAndroid.app.AlertDialog.Builder;6 ImportAndroid.app.AlertDialog;7 ImportAndroid.content.DialogInterface;8 9 Public classMainactivityextendsActivity {Ten One @Override A protected voidonCreate (Bundle savedinstancestate) { - Super. OnCreate (savedinstancestate); - Setcontentview (r.layout.activity_main); the // - // //to create a alertdialog, use the Create () method in the Alertdialog.builder - //Builder alertdialog = new Alertdialog.builder (this); - //alertdialog.settitle ("Simple dialog box"); //Set Title + //Alertdialog.setmessage ("Once Upon a man, he was cold, and at last he died of cold." "); //Set Content - //Alertdialog.seticon (R.mipmap.ic_launcher); //Set icon + //alertdialog.create (); A //alertdialog.show (); at -Builder dialog =NewAlertdialog.builder ( This); -Dialog.settitle ("OK delete?") "); -Dialog.setmessage ("Are you sure you want to delete this piece of information?" "); - Dialog.seticon (r.mipmap.ic_launcher); - in //Register listener events for the Cancel button -Dialog.setnegativebutton ("Cancel",NewDialoginterface.onclicklistener () { to @Override + Public voidOnClick (Dialoginterface Dialog,intwhich) { - //... the } * }); $ Panax Notoginseng //Register Listener events for the "View Details" button -Dialog.setneutralbutton ("View Details",NewDialoginterface.onclicklistener () { the @Override + Public voidOnClick (Dialoginterface Dialog,intwhich) { A //... the } + }); - $ //registering listener events for the OK button $Dialog.setpositivebutton ("OK",NewDialoginterface.onclicklistener () { - @Override - Public voidOnClick (Dialoginterface Dialog,intwhich) { the //... - }Wuyi }); the - dialog.create (); Wu dialog.show (); - } About $ -}
Operating effect:
Description:Setpositivebutton (),Setnegativebutton (),Setneutralbutton ( ) methods are used to set the OK button, Cancel button, Some properties of the middle button, the position of the three buttons is relatively fixed in the Android2.3 of the following platforms, respectively, to the left, right and center, However, in the Android4.0 above platform, Setpositivebutton and Setnegativebutton position opposite, respectively, on the right and left.
(3) Alertdialog dialog box with radio button or check button
Directly on the code:
1 PackageCom.example.administrator.dialog1;2 3 Importandroid.app.Activity;4 ImportAndroid.os.Bundle;5 ImportAndroid.app.Dialog;6 ImportAndroid.app.AlertDialog.Builder;7 ImportAndroid.app.AlertDialog;8 ImportAndroid.content.DialogInterface;9 ImportAndroid.widget.Toast;Ten One Public classMainactivityextendsActivity { A - Private intSelectedcityindex = 0; - the @Override - protected voidonCreate (Bundle savedinstancestate) { - Super. OnCreate (savedinstancestate); - Setcontentview (r.layout.activity_main); + - //define a city array + Finalstring[] Arraycity =NewString[] {"Beijing", "Shanghai", "Guangzhou", "Shenzhen" }; A at //Instantiate alertdialog dialog box -Dialog Alertdialog =NewAlertdialog.builder ( This) -. Settitle ("which place do you like best?") ")//Set Title -. SetIcon (R.mipmap.ic_launcher)//Set icon - //The Settings dialog box displays a single-selection list, specifying the default check, and setting the listener event handling -. Setsinglechoiceitems (arraycity, 0,NewDialoginterface.onclicklistener () { in - @Override to Public voidOnClick (Dialoginterface Dialog,intwhich) { +Selectedcityindex = which;//The index of the selected item is saved to the selected item variable - } the }) * //Add a Cancel button and increase listening processing $. Setnegativebutton ("Cancel",NewDialoginterface.onclicklistener () {Panax Notoginseng @Override - Public voidOnClick (Dialoginterface Dialog,intwhich) { the //TODO auto-generated Method Stub + } A }) the //Add a OK button and increase listening processing +. Setpositivebutton ("Confirm",NewDialoginterface.onclicklistener () { - @Override $ Public voidOnClick (Dialoginterface Dialog,intwhich) { $ Toast.maketext (Getapplication (), Arraycity[selectedcityindex], Toast.length_short). Show (); - } - }) the . Create (); - alertdialog.show ();Wuyi } the -}
Description: Setsinglechoiceitems (charsequence[] items, int checkeditem,final Onclicklistener listener) method to implement a ListView-like Alertdialog, the first parameter is an array of data to display, the second parameter specifies the default check, and the third parameter sets the listener handling event.
Operating effect:
11.Android Common dialog Box Alertdialog learning