Create dialog box creating dialogs
A dialog box is usually a small window before the current activity. The following activities lose focus and the dialog box accepts all user interactions. A dialog box is usually used as a notification or running application.ProgramRelated activities.
Android API supports the following dialog box object types:
Warning dialog box alertdialog
This dialog boxManagement0, 1, 2, or 3 buttons, and/or a list of options that can contain check boxes and radio buttons. This warning dialog box can form most user interfaces and is a recommended dialog box type. See the following create a warning dialog box creating an alertdialog.
Progress dialog box progressdialog
Displays a progress wheel or progress bar. Therefore, it is an extension of the warning dialog box, and it also supports buttons. See the following creating a progressdialog.
Date selection dialog box datepickerdialog
A dialog box that allows users to select a date. Please refer to the hello datepicker guide.
Time Selection dialog box timepickerdialog
A dialog box that allows users to select a time. Please refer to the hello timepicker guide.
If you want to customize your own dialog box, you can expand and define a new layout on the basic dialog box object or any subclass dialog box listed above. See the section creating a custom dialog in the create custom dialog box below.
Show dialog box showing a dialog
A dialog box is often created and displayed as part of an activity. You should usually create a dialog box from the oncreatedialog (INT) callback method of the activity. When you use this callback function, the android system will effectively set this activity as the owner of each dialog box, so as to automatically manage the status of each dialog box and link it to the activity. In this way, each dialog box inherits the specific attributes of this activity. For example, when a dialog box is opened, the menu key displays the option menu defined for this activity, and the volume key modifies the audio stream used by the activity.
Note: If you decide to create a dialog box outside the oncreatedialog () method, it will not be attached to the activity. However, you can use setowneractivity to attach it to an activity.
To display a dialog box, call the showdialog (INT) method and pass an integer that uniquely identifies the dialog box.
When the dialog box is requested for the first time, Android calls oncreatedialog (INT) from your activity. You should initialize the dialog box dialog here. This callback method is passed to the same ID as showdialog (INT. After you create this dialog box, return this object at the end of the method.
Before the dialog box is displayed, Android also calls the optional callback function onpreparedialog (INT, DIALOG ). if you want to change any of its attributes when each dialog box is opened, you can define this method. This method is called each time a dialog box is opened, while oncreatedialog (INT) is called only when the dialog box is opened for the first time. If you do not define onpreparedialog (), this dialog box will remain the same as the previous one. This method is also passed with the dialog box ID and the dialog box object created in oncreatedialog.
The best way to define the oncreatedialog (INT) and onpreparedialog (INT, DIALOG) callback functions is to use a switch statement to check the passed ID parameter. Each case should check a unique dialog box ID and then create and define the corresponding dialog box. For example, imagine that a game uses two different dialogs: one to indicate that the game has been suspended and the other to indicate that the game has ended. First, define an integer for each dialog box:
Static final int dialog_paused_id = 0;
Static final int dialog_gameover_id = 1;
Then, define the oncreatedialog (INT) callback function for each ID using a switch case:
Protected dialog oncreatedialog (int id ){
Dialog dialog;
Switch (ID ){
Case dialog_paused_id:
// Do the work to define the pause Dialog
Break;
Case dialog_gameover_id:
// Do the work to define the game over Dialog
Break;
Default:
Dialog = NULL;
}
Return dialog;
}
Note: In this example, the case statement has no specific content, because it is beyond the scope of this chapter.
When one of the dialog boxes is displayed, use the dialog box ID to call showdialog (INT ):
Showdialog (dialog_paused_id );
Delete dialog box dismissing a dialog
When you want to close the dialog box, you can call dismiss () to delete it. If needed, you can also call the dismissdialog (INT) method from this activity, which will actually call the dismiss () method for this dialog box.
If you want to use the oncreatedialog (INT) method to manage the status of your dialog box (as discussed in the previous chapter), and each time your dialog box is cleared, the status of the dialog box objects will be retained by the activity. If you decide that you no longer need this object or clear the state, you should call removedialog (INT ). This will delete any internal object reference. If this dialog box is displayed, it will be removed.
Use the using dismiss listeners
If you want your application to execute some processes when a dialog box disappears, you should attach an on-dismiss listener to the dialog box.
First, define the dialoginterface. ondismisslistener interface. This interface has only one method, ondismiss (dialoginterface), which will be called when the dialog box disappears. Then, pass your ondismisslistener to setondismisslistener ().
However, note that the dialog box can also be canceled ". This is a special case that indicates that the dialog box is canceled by the user. This will occur when you press the "return" button, or the cancel () call shown in this dialog box (maybe through a "cancel" button on the dialog box ). When a dialog box is canceled, this ondismisslistener will still be notified, but if you want to be notified when the dialog box is canceled (rather than the usual elimination method ), you should register a dialoginterface through setoncancellistener. oncancellistener.
Create alarm dialog box creating an alertdialog
A warning dialog box is an extension class of the dialog box. It can build most Dialog Box user interfaces and is a recommended dialog box type. You should use it when you have the following features:
· A title
· A text message
· 1, 2, or 3 buttons
· A list of Optional options (optional check boxes or single-choice buttons)
To create a warning dialog box, use the alertdialog. Builder subclass. Use alertdialog. Builder (context) to obtain a constructor and use the public methods of this class to define all the attributes of the warning dialog box. After obtaining the constructor, you can use the CREATE (). method to obtain the alert dialog box object.
The following describes how to use the alertdialog. Builder class to define different warning dialog box attributes. If you use the followingCodeYou can return the result dialog box object to display it.
Add button adding buttons
To create a warning dialog box containing parallel buttons shown in the right image, use the set... button () method:
Alertdialog. Builder = new alertdialog. Builder (this );
Builder. setmessage ("are you sure you want to exit? ")
. Setcancelable (false)
. Setpositivebutton ("yes", new dialoginterface. onclicklistener (){
Public void onclick (dialoginterface dialog, int ID ){
Myactivity. This. Finish ();
}
})
. Setnegativebutton ("no", new dialoginterface. onclicklistener (){
Public void onclick (dialoginterface dialog, int ID ){
Dialog. Cancel ();
}
});
Alertdialog Alert = builder. Create ();
First, add a message setmessage (charsequence) to this dialog box ). Then, start function chain and set this dialog box to not canceled not cancelable (therefore, you cannot use the return button to close this dialog box ). For each button, use any set... button () method, such as setpositivebutton (). This method accepts the button name and a dialoginterface. onclicklistener that defines the action taken after the selected button.
Note: You can only add one of the button types for this warning dialog box. That is, you cannot include multiple "OK" buttons. This limits the number of possible buttons to only three: OK, neutral, and negative. These names are technically irrelevant to the actual functions of your buttons, but they should help you record what you have done.
Add a list adding a list
To create a warning dialog box with a list of options, as shown on the right, you can use the setitems () method:
Final charsequence [] items = {"red", "green", "blue "};
Alertdialog. Builder = new alertdialog. Builder (this );
Builder. settitle ("pick a color ");
Builder. setitems (items, new dialoginterface. onclicklistener (){
Public void onclick (dialoginterface dialog, int item ){
Toast. maketext (getapplicationcontext (), items [item], Toast. length_short). Show ();
}
});
Alertdialog Alert = builder. Create ();
First, use the settitle (charsequence) method to add a title to the dialog box. Then, you can use setitems () to add an Option List that accepts a set of displayed items and a dialoginterface. onclicklistener to define the actions taken after the selected button.
Add check box and radio button
To create a checkboxes or radio buttons in the dialog box, call the setmultichoiceitems () and setsinglechoiceitems () methods respectively. If you create these optional lists in the oncreatedialog () callback function, Android will help you manage the list status. As long as this activity is activated, the dialog box will remember the previously selected items, but if the user exits this activity, the user will lose the selection.
Note: To save the selection when the user leaves or suspends the activity, you must use the activity lifecycle to properly save and restore the settings. To save the option permanently, you need to use data storage technology even if the active process is completely terminated.
To create a warning dialog box containing the list of single options shown on the right, use the same code in the previous example. However, you must replace the setitems () method with setsinglechoiceitems ().
Final charsequence [] items = {"red", "green", "blue "};
Alertdialog. Builder = new alertdialog. Builder (this );
Builder. settitle ("pick a color ");
Builder. setsinglechoiceitems (items,-1, new dialoginterface. onclicklistener (){
Public void onclick (dialoginterface dialog, int item ){
Toast. maketext (getapplicationcontext (), items [item], Toast. length_short). Show ();
}
});
Alertdialog Alert = builder. Create ();
The second parameter of setsinglechoiceitems () is a checkeditem integer value, indicating the position of the default selection item based on 0. "-1" indicates that no default options are available.
Create progress dialog box creating a progressdialog
The progress dialog box progressdialog is an extension of the alertdialog class. It displays a progress animation in the rotation wheel shape for an undefined progress task or a progress bar for a specified progress task. This dialog box also provides buttons, such as a button to cancel the download.
You can simply call progressdialog. Show () to display a progress dialog box. For example, you can easily get the progress dialog box displayed on the right, instead of using the oncreatedialog (INT) callback to manage this dialog box, as shown below:
Progressdialog dialog = progressdialog. Show (myactivity. This, "", "loading. Please wait...", true );
The first parameter is the context of the application, the second is the dialog box title (null here), and the third is the information, finally, this parameter indicates whether the progress is uncertain (this is only related to the creation progress bar, which will be described in the next chapter ).
The default type of the Progress dialog box is a rotation wheel. If you want to create an interval progress, you need more code, as described in the following chapter.
Show progress bar showing a progress bar
Use the animation progress bar to display the progress:
1. Use the class constructor to initialize the progress dialog box, progressdialog (context ).
2. Use setprogressstyle (INT) to set the progress style to "style_horizontal" and set other attributes, such as messages.
3. When you are about to display this dialog box, call show () or return progressdialog from the oncreatedialog (INT) callback.
4. You can call setprogress (INT) to set the current progress percentage or call the incrementprogressby (INT) method to increase the progress value.
For example, your settings may look like this:
Progressdialog;
Progressdialog = new progressdialog (mcontext );
Progressdialog. setprogressstyle (progressdialog. style_horizontal );
Progressdialog. setmessage ("loading ...");
Progressdialog. setcancelable (false );
The setting is simple. Most of the Creation code is also used to update the progress. You may realize that it is necessary to create another thread to complete the progress report. The progress is returned to the active user interface thread through an object.
Code
Package Com. Amaker. test;
Import Android. App. activity;
Import Android. App. alertdialog;
Import Android. content. dialoginterface;
Import Android. OS. Bundle;
Import Android. View. view;
Import Android. View. View. onclicklistener;
Import Android. widget. Button;
Import Android. widget. textview;
/**
*
* Alertdialog
*/
Public Class Mainactivity Extends Activity {
Private Textview mytv;
Private Button mybtn;
@ Override
Public Void Oncreate (bundle savedinstancestate ){
Super . Oncreate (savedinstancestate );
Setcontentview (R. layout. Main );
Mytv = (Textview) findviewbyid (R. Id. textview01 );
Mybtn = (Button) findviewbyid (R. Id. button01 );
Final Alertdialog. Builder = New Alertdialog. Builder ( This );
Mybtn. setonclicklistener ( New Onclicklistener (){
Public Void Onclick (view v ){
Builder. setmessage ( " Are you sure you want to delete this record? " ). Setpositivebutton ( " Yes " , New Dialoginterface. onclicklistener (){
Public Void Onclick (dialoginterface dialog, Int Which ){
Mytv. settext ( " Deleted successfully! " );
}
}). Setnegativebutton ( " No " , New Dialoginterface. onclicklistener (){
Public Void Onclick (dialoginterface dialog, Int Which ){
Mytv. settext ( " Cancel deletion! " );
}
});
Alertdialog ad = Builder. Create ();
Ad. Show ();
}
}
);
}
}
Code
Package Com. Amaker. test;
Import Android. App. activity;
Import Android. App. alertdialog;
Import Android. content. dialoginterface;
Import Android. OS. Bundle;
Import Android. View. view;
Import Android. View. View. onclicklistener;
Import Android. widget. Button;
Import Android. widget. textview;
Public Class Mainactivity Extends Activity {
Private Textview mytv;
Private Button mybtn;
Public Void Oncreate (bundle savedinstancestate ){
Super . Oncreate (savedinstancestate );
Setcontentview (R. layout. Main );
Mytv = (Textview) findviewbyid (R. Id. textview01 );
Mybtn = (Button) findviewbyid (R. Id. button01 );
Final String [] items = { " Pig " , " Spicy chicken leg fort " , " Coffee " };
Final Alertdialog. Builder = New Alertdialog. Builder ( This );
Mybtn. setonclicklistener ( New Onclicklistener (){
Public Void Onclick (view v ){
// Builder. settitle ("order"). setitems (items, new dialoginterface. onclicklistener (){
// If it is changed to the following method, It will be displayed in a single button style
Builder. settitle ( " Order " ). Setsinglechoiceitems (items, - 1 , New Dialoginterface. onclicklistener (){
Public Void Onclick (dialoginterface dialog, Int Which ){
Mytv. settext (items [which]);
}
});
Alertdialog ad = Builder. Create ();
Ad. Show ();
}
});
}
}