In this example, the main Activity is defined in AlertDialogSamples. java to introduce the usage of AlertDialog. AlertDialog provides a variety of functions:
Displays the message to the user, and provides one or three buttons (OK, Cancel, Yes, No) for selection or display warning.
A list is displayed for the user to choose from. The list can be a Radio Button (single choice) or a Check button (multiple choice)
Display the text box to accept user input.
Creating AlertDialog is generally constructed through AlertDialog. Builder:
[Java]
AlertDialog. Builder ad = new AlertDialog. Builder (context );
AlertDialog. Builder ad = new AlertDialog. Builder (context );
You can set the title, displayed information, and the Buttons to be displayed for this AlergDialog. Call ad. show to display this dialog box.
To avoid creating a Dialog object for each displayed Dialog box, Android provides two methods: onCreateDialog and onPrepareDialog to manage the creation of the Dialog box.
By reloading onCreateDialog, you can create a required dialog box instance as needed (such as when showDialog is executed. After creating the dialog box instance, you can reload the onPrepareDialog method to modify the dialog box before showDialog. Similar to how Android manages menus.
The following describes how to use AlertDialog. Because onCreateDialog may create multiple Dialog examples, you must first define a Dialog ID.
[Java] view plaincopyprint?
Private static final int DIALOG_YES_NO_MESSAGE = 1;
Private static final int DIALOG_YES_NO_MESSAGE = 1;
Reload onCreateDialog. The parameter id is the Dialog ID. You can create a required Dialog instance based on the id.
[Java]
@ 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 ();
...
@ 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
[Java]
ShowDialog (DIALOG_YES_NO_MESSAGE );
ShowDialog (DIALOG_YES_NO_MESSAGE );
App-> Dialog describes multiple AlertDialog usage using eight examples:
OK Cancel dialog with a message
The code shown above is the code in this example. Use AlertDialog. builder creates an AlertDialog example and uses setIcon, setTitle, setPositiveButton, and setNegativeButton to set the icons, titles, OK, and Cancel buttons of the dialog box. The title does not know the language:
OK Cancel dialog with a long message
In this example, setMessage is used to display a long message (a scroll bar may be available), and an intermediate button is added through setNeutralButton.
List Dialog
AlertDialog can be used to display a set of options for users to select. For static options, you can first define an Array Resource:
<! -Used in app/dialog examples->
<String-array name = "select_dialog_items">
<Item> Command one </item>
<Item> Command two </item>
<Item> Command three </item>
<Item> Command four </item>
</String-array>
Then, setItems is called to display these options:
[Java]
1. case DIALOG_LIST:
2. return new AlertDialog. Builder (AlertDialogSamples. this)
3. setTitle (R. string. select_dialog)
4. setItems (R. array. select_dialog_items, new DialogInterface. OnClickListener (){
5. public void onClick (DialogInterface dialog, int which ){
6.
7./* User clicked so do some stuff */
8. String [] items = getResources (). getStringArray (R. array. select_dialog_items );
9. new AlertDialog. Builder (AlertDialogSamples. this)
10. setMessage ("You selected:" + which + "," + items [which])
11. show ();
12 .}
13 .})
14. create ();
Progress dialog
This example shows the usage of ProgressDialog. ProgressDialog is a subclass of AlertDialog. ProgressDialog does not need to be constructed through AlertDialog. Builder. You can directly create a ProgressDialog instance through the constructor. ProgressDialog displays a title and a progress bar. Therefore, there are several more methods than AlertDialog: setProgressStyle, setMax, and so on to configure the properties of the progress bar.
-(.
Note: In this example, Handler is used to update the progress bar. Handler will introduce it in the following example.
[Java]
1. case DIALOG_PROGRESS:
2. mProgressDialog = new ProgressDialog (AlertDialogSamples. this );
3. mProgressDialog. setIcon (R. drawable. alert_dialog_icon );
4. mProgressDialog. setTitle (R. string. select_dialog );
5. mProgressDialog. setProgressStyle (ProgressDialog. STYLE_HORIZONTAL );
6. mProgressDialog. setMax (MAX_PROGRESS );
7. mProgressDialog. setButton (getText (R. string. alert_dialog_hide ),
8. new DialogInterface. OnClickListener (){
9. public void onClick (DialogInterface dialog, int whichButton ){
10.
11./* User clicked Yes so do some stuff */
12 .}
13 .});
14. mProgressDialog. setButton2 (getText (R. string. alert_dialog_cancel ),
15. new DialogInterface. OnClickListener (){
16. public void onClick (DialogInterface dialog, int whichButton ){
17.
18./* User clicked No so do some stuff */
19 .}
20 .});
21. return mProgressDialog;
Single choice list
When AlertDialog is used to display a list, you can specify one or more options. Change the setItems method in the previous List to setSingleChoiceItems. You can use RadioButton to display the List:
[Java]
1. case DIALOG_SINGLE_CHOICE:
2. return new AlertDialog. Builder (AlertDialogSamples. this)
3. setIcon (R. drawable. alert_dialog_icon)
4. setTitle (R. string. alert_dialog_single_choice)
5. setSingleChoiceItems (R. array. select_dialog_items2, 0,
6. new DialogInterface. OnClickListener (){
7. public void onClick (DialogInterface dialog, int whichButton ){
8.
9./* User clicked on a radio button do some stuff */
10 .}
11 .})
12. setPositiveButton (R. string. alert_dialog_ OK,
13. new DialogInterface. OnClickListener (){
14. public void onClick (DialogInterface dialog, int whichButton ){
15.
16./* User clicked Yes so do some stuff */
17 .}
18 .})
19. setNegativeButton (R. string. alert_dialog_cancel,
20. new DialogInterface. OnClickListener (){
21. public void onClick (DialogInterface dialog, int whichButton ){
22.
23./* User clicked No so do some stuff */
24 .}
25 .})
26 .. create ();
Repeat alarm
In this example, the setMultiChoiceItems method is called and the CheckButton is used to display the list, indicating that multiple
[Java]
1. case DIALOG_MULTIPLE_CHOICE:
2. return new AlertDialog. Builder (AlertDialogSamples. this)
3. setIcon (R. drawable. ic_popup_reminder)
4. setTitle (R. string. alert_dialog_multi_choice)
5. setMultiChoiceItems (R. array. select_dialog_items3,
6. new boolean [] {false, true, false, true, false },
7. new DialogInterface. OnMultiChoiceClickListener (){
8. public void onClick (DialogInterface dialog,
9. int whichButton,
10. boolean isChecked ){
11.
12./* User clicked on a check box do some stuff */
13 .}
14 .})
15. setPositiveButton (R. string. alert_dialog_ OK,
16. new DialogInterface. OnClickListener (){
17. public void onClick (DialogInterface dialog, int whichButton ){
18.
19./* User clicked Yes so do some stuff */
20 .}
21 .})
22. setNegativeButton (R. string. alert_dialog_cancel,
23. new DialogInterface. OnClickListener (){
24. public void onClick (DialogInterface dialog, int whichButton ){
25.
26./* User clicked No so do some stuff */
27 .}
28 .})
29 .. create ();
Send Call to VoiceMail
In the preceding example, the list items are implemented by defining Array resources. The list items can also use Content Provider as the data source. In this example, the record in the address book is used as the data source. If you are running in the simulator, add several Contacts first. For Content Provider introduction, see Android ApiDemo example resolution (10): App-> Activity-> QuickContactsDemo
[Java]
1. case DIALOG_MULTIPLE_CHOICE_CURSOR:
2. String [] projection = new String [] {
3. Contacts. People. _ ID,
4. Contacts. People. NAME,
5. Contacts. People. SEND_TO_VOICEMAIL
6 .};
7. Cursor cursor = managedQuery (Contacts. People. CONTENT_URI,
8. projection, null );
9. return new AlertDialog. Builder (AlertDialogSamples. this)
10 .. setIcon (R. drawable. ic_popup_reminder)
11. setTitle (R. string. alert_dialog_multi_choice_cursor)
12. setMultiChoiceItems (cursor,
13. Contacts. People. SEND_TO_VOICEMAIL,
14. Contacts. People. NAME,
15. new DialogInterface. OnMultiChoiceClickListener (){
16. public void onClick (DialogInterface dialog,
17. int whichButton,
18. boolean isChecked ){
19. Toast. makeText (AlertDialogSamples. this,
20. "Readonly Demo Only
21.-Data will not be updated ",
22. Toast. LENGTH_SHORT). show ();
23 .}
24 .})
25. create ();
Text Entry Dialog
The last example is to use a text box to accept user input. AlertDialog also allows custom UI. You can use custom Layout as the UI of AlertDialog. Theoretically, any Layout can be used to avoid customizing the Dialog box using the derived Dialog method. In this example, alert_dialog_text_entry is used. The two textviews in the definition display the Name: And Password: tags. The two edittexts are input by the user. Then, use setView to define the custom UI for AlertDialog.
[Java]
1. case DIALOG_TEXT_ENTRY:
2. // This example shows how to add a custom layout to an AlertDialog
3. LayoutInflater factory = LayoutInflater. from (this );
4. final View textEntryView
5. = factory. inflate (R. layout. alert_dialog_text_entry, null );
6. return new AlertDialog. Builder (AlertDialogSamples. this)
7 .. setIcon (R. drawable. alert_dialog_icon)
8. setTitle (R. string. alert_dialog_text_entry)
9. setView (textEntryView)
10. setPositiveButton (R. string. alert_dialog_ OK,
11. new DialogInterface. OnClickListener (){
12. public void onClick (DialogInterface dialog, int whichButton ){
13.
14./* User clicked OK so do some stuff */
15 .}
16 .})
17. setNegativeButton (R. string. alert_dialog_cancel,
18. new DialogInterface. OnClickListener (){
19. public void onClick (DialogInterface dialog, int whichButton ){
20.
21./* User clicked cancel so do some stuff */
22 .}
23 .})
24. create ();
Author: mapdigit