The main activity definition of this example is used primarily in Alertdialogsamples.java to introduce the use of Class Alertdialog, Alertdialog provides a variety of functions:
Displays the message to the user and provides one to three buttons (OK, Cancel, Yes, No) to select or display a warning.
Displays a list for the user to choose from, the list can be radio button (radio), Check button (multiple selection)
Displays a text box to accept user input, and so on.
Creating Alertdialog is typically constructed by Alertdialog.builder:
Alertdialog.builder ad=new Alertdialog.builder
(context);
After that, you can set the caption for this alergdialog, display the information, need to display the buttons and so on. Then call Ad.show to display the dialog box.
To avoid creating a new dialog object each time the dialog box is displayed, Android provides two methods Oncreatedialog and Onpreparedialog events to manage the creation of the dialog box.
By overloading Oncreatedialog, you can create the required dialog box instances as needed, such as when performing ShowDialog. After you create this dialog instance, you can overload the Onpreparedialog method before each ShowDialog, if you need to make some changes to the dialog box. The principle is similar to the way Android manages menu.
The general steps for using Alertdialog are given below. Because it is possible to create multiple dialog examples in Oncreatedialog, you must first define the ID of a dialog.
private static final int dialog_yes_no_message = 1;
The Oncreatedialog is then overloaded, the parameter ID is dialog ID, and the desired dialog instance can be created based on the ID.
@Override
protected Dialog oncreatedialog (int id) {
switch (ID) {case
dialog_yes_no_message:
return New Alertdialog.builder (Alertdialogsamples.this)
. SetIcon (R.drawable.alert_dialog_icon)
. SetTitle ( R.string.alert_dialog_two_buttons_title)
. Setpositivebutton (R.STRING.ALERT_DIALOG_OK,
new Dialoginterface.onclicklistener () {public
void OnClick (dialoginterface dialog, int whichbutton) {/
* User Clicked OK so do some stuff
/}
})
. Setnegativebutton (R.string.alert_dialog_cancel,
new Dialoginterface.onclicklistener () {public
void OnClick (dialoginterface dialog, int whichbutton) {/
* User Clicked Cancel so do some stuff
/}
)
. Create ();
...
Show dialog
ShowDialog (Dialog_yes_no_message);
App->dialog uses eight examples to illustrate Alertdialog's multiple usages:
OK Cancel dialog with a message