What is Dialog?
The dialog class is the base class for all dialogs. It is important to note that although the dialog class can be displayed on the interface, it is not inherited from the view class, but is constructed directly from the Java.lang.Object.
Like activity, Dialog also has a life cycle, and its life cycle is maintained by activity. Activity is responsible for generating, saving, and recovering it.
There are a number of callback functions at each stage of the life cycle that the system calls backwards.
The functions that the user can actively invoke in activity are:
@ showDialog (int id) is responsible for displaying dialog that are identified as IDs. If this function is called, the system will reverse call dialog's callback function oncreatedialog (int id).
@ dismissdialog (int id) causes the dialog identified as ID to disappear in the interface.
Dialog has two more common callback functions, oncreatedialog (int id) and onpreparedialog (int id,dialog Dialog) functions. When you call ShowDialog (int id) in activity , if this Dialog is the first generation, the system will reverse call Dialog's callback function oncreatedialog (int id), and then call Onpreparedialog (int id,dialog Dialog); If the Dialog has been generated, but not yet displayed, then the oncreatedialog (int id) will not be recalled, but the onpreparedialog (int id, Dialog Dialog) method is directly callback.
The Onpreparedialog (int id,dialog Dialog) method provides a mechanism that, when Dialog is generated but not displayed, gives the opportunity to make some changes to the Dialog before the display, such as the Dialog title.
After understanding some basic knowledge of dialog, take a look at the implementation code in the OnCreate () method in the Activitymain file:
Button BT1 = (button) Findviewbyid (R.id.button1); Bt1.setonclicklistener (new Onclicklistener () {public void OnClick (View v) { showDialog (DIALOG1); }});
Code Explanation:
The Findviewbyid method returns a reference to this component through the component's ID.
The Setonclicklistener method sets a click Listener for BT1.
OnClick () is the callback function after clicking the button.
ShowDialog () is the function inside the activity. Responsible for displaying the dialog with ID DIALOG1.
After executing the ShowDialog (DIALOG) code, the system executes the callback function Oncreatedialog (), which implements the following code:
protected Dialog oncreatedialog (int id) { switch (ID) {case DIALOG1: return BuildDialog1 ( Activitymain.this); Case DIALOG2: return buildDialog2 (activitymain.this); Case DIALOG3: return BuildDialog3 (activitymain.this); } return null;}
Code Explanation:
For different dialog IDs, different dialog are generated.
The BUILDDIALOG1 function generates the first dialog to be displayed.
The Builddialog parameter is context.
Next look at the BuildDialog1 () function, which implements the following code:
Private Dialog BuildDialog1 (context context) { Alertdialog.builder Builder = new Alertdialog.builder (context); Builder.seticon (R.drawable.alert_dialog_icon); Builder.settitle (r.string.alert_dialog_two_buttons_title); Builder.setpositivebutton (r.string.alert_dialog_ok,new dialoginterface.onclicklistener () {public void OnClick (dialoginterface dialog,int whichbutton) { settitle ("clicked OK") } ); Builder.setnegativebutton (r.string.alert_dialog_cancel,new dialoginterface.onclicklistener () { public void OnClick (Dialoginterface dialog,int whichbutton) { settitle ("clicked Cancel");} ); return Builder.create (); }
Code Explanation:
The Alertdialog.builder builder = new Alertdialog.builder (context) statement first generates a Alertdialog.builder object so that you can start constructing alertdialog.
The Builder.seticon (R.DRAWABLE.ALERT_DIALOG_ICON) statement sets a picture to Alertdialog. Here is the alert_dialog_icon.png picture.
The Builder.settitle (r.string.alert_dialog_two_buttons_title) statement gives Alertdialog a title, The string contents of the caption are defined in the String.xml file of the value directory.
Setpositivebutton () This method sets some properties of the OK button. The first parameter is what is displayed on the button, and the second parameter is the Dialoginterface.onclicklistener () listener object.
The OnClick () method is the callback method in the listener, that is, when the dialog button is clicked, the system callbacks the method. In this callback function, we re-set the contents of the title bar by the Settitle method.
The Setnegativebutton () method corresponds to the Setpositivebutton () method.
=================
Alertdialog and Alertdialog.builder:
Alertdialog is a direct subclass of dialog, and Alertdialog is one of the most commonly used dialog boxes on the Android system.
A alertdialog can have two or three buttons, and you can set the title and message to a alertdialog.
You cannot generate a alertdialog directly from the Alertdialog constructor. The general generation of Alertdialog is constructed by an internal static class Alertdialog.builder.
Builder.setmessage (R.string.alert_dialog_msg) This method is to set some information (message) below the title.
The Setneutralbutton () method sets some of the properties of the middle button, with the same settings as Setpositivebutton () and Setnegativebutton ().
=================
After all the attributes of dialog are preset, the execution of Builder.create () becomes a well-configured dialog.
When the program finishes executing the Oncreatedialog () method, the Onpreparedialog method is then executed. In the Onpreparedialog () method, the dialog can be modified.
As an example:
protected void Onpreparedialog (int id, Dialog Dialog) { if (id = DIALOG1) { settitle ("changed to:");} }
At this point the title will be changed.
Android's Dialog