In Android, dialog is a very important UI, it can be convenient for users to prompt, in the most concise way to show the user information, the following picture is the dialog of a whole structure, through it, you can have a clear understanding of dialog.
As you can see from this diagram, the dialog is the parent class, under which the most important, our most commonly used alertdilog, and the Alertdialog subclass, is composed of Datpicker, Progressdialog,timepick.
These sub-categories are the most common in our program development, so we can try to think about the answers to some of the questions below.
<1> How to produce a dialog?
What is the relationship between <2> dialog and activity? Why does the activity lose focus when it shows dialog, cannot be clicked, and how does it manage their relationship inside the system?
We take ProgressDialog as an example, through the analysis of its source code, let us thoroughly understand its internal operating mechanism
<1> how to produce a progressdialog?
By opening the source of the ProgressDialog, you can see that it has 2 construction methods,
ProgressDialog (Context context)
ProgressDialog (Context context,int theme)
Parameter explanation: Context: Indicates that it relies on the context, must create it through a context, the second parameter theme: this dialog theme.
In addition we can also find that there are four static show methods, they are only different parameters,
Show (Context context,charsequence title,charsequence message);
Show (Context context,charsequence title,charsequence message,boolean indeterminate);
At this time, we just look at the last static method, because three static methods will end up calling the last show static method with the most parameters to complete the function.
The functions it implements are:
ProgressDialog dialog=new Progressdiaog (context);
...
...
...
Dialog.show ();
This is actually the same as we call the constructor method.
To this step, just to know, how to invoke the resulting progressdialog, but the specifics of the interior. Also see OnCreate () method, this method is really able to produce progressdialogr place, it is the same as the OnCreate () method in the activity, is the initialization of the call, we look at its source code as follows:
First See is a named Mviewupdatehandler Handler, the Handler is mainly used to update the value of progress, (percentage), and then look down ...
There is a sentence: View view=inflater.inflate (R.layout.alert_dialog_press,null);
Obviously, this is what we see in the dialog, which shows in the transit ring. However, this Alert_dialog_pross file is in the COM.ANDROID.INTERNAL.R package and belongs to the system-defined UI configuration file.
Finally, call the Setview (view) method to display the view on the dialog.
Therefore, we can also implement a custom Dialog.xml file to invoke the Setview () method to display it.
This Setview () method is defined in the Alertdialog class, and will be explained later!
This is the progressdialog that we usually see. The analysis is complete.
Alertdialog
Next, we begin to analyze ProgressDialog's parent class Alertdialog.
Open the source code, the first thing to be greeted is three construction methods, but these three construction methods are protected type,
As we can see, we do not allow direct instantiation of Alertdialog. So, let's see if there's any other way. can be instantiated
Look carefully and find a variable,
Alertcontroller Malert; This is the main character of our day, focus on it.
The definition of Malert is in the constructor of the alertdialog that we mentioned above,
In addition, we have found that almost all of the methods in Alertdialog are manipulated by this malert variable,
In other words, Alertdialog is an empty shell, and it has no practical effect, its essence is Alertcontroller class.
Is the "protagonist" I just said. and Alertcontroller source, is the Android system comes with, we need to spend a lot of effort to find.
Looking down, the Alertdialog class finds a static class Builder, and through this class, we can also create a dialog directly,
Builder Static class also has a Alertcontroller.alertparams class of object P, we look at the builder's source code, it can set all the dialog methods, including
Settitle (charsequence title)
Setmessage (int messageId);
SetIcon (int iconid);
Setpositivebutton ()
...
...
....
And so, we look at the internal implementation of these methods, all for its built-in object p to implement, that is, we call these methods, the end result is actually used in the Alertcontroller.alertparams class of object P body,
, and there is an interesting phenomenon, the return values of these methods are builder objects, that is to say, we can call this method at the same level,
To show a dialog, just call these set methods, not enough, and finally one step, is to call the Create () method, this method is the last call, that is, create a dialog box, we see that the creation method has done those actions
Public Alertdialog Create () {
Final Alertdialog Dialog =new Alertdialog (p.mcontext);
P.apply (Dialog.malert)
Dialog.setcancelable (p.mcancelable);
return dialog;
}
This is the implementation of Create (), which we can intuitively see is that it helped us build a Alertdialog object,
The p here is the object of the alertcontroller.alertparams we created,
And in Alertdialog dialog=new Alertdialog (p.mcontext); In this code, the definition of dialog also defines a Alertcontroller object Malert;
In the second code, p.apply (Dialog.malert), we track the past, look down, haha, the original is, we just call the value of the set method, and restore to the new definition of the Malert object in the dialog, this understanding, the original p is just a "fart", Temporarily the object that holds the data, as long as we call the Create method, p will obediently give the data to the dialog object,
In addition to the Create method, there is a show () method that implements the following
Public Alertdialog Show () {
Alertdialog dialog=create ();
Dialog.show ();
return dialog;
}
As we can see, show () is also called the Create method.
From the following analysis, we can easily understand, as follows our commonly used statements,
return new Alertdialog.builder (context).
SetIcon (R.drawable.alert_icon).
Settitle (R.STRING.ALERT_STR)
. Create ();
This is what we usually use to produce dialog.
Android in Dialog