In Android, dialog is a very important UI, which can easily prompt users and display information in the most concise way. The following figure shows the overall structure of dialog, it provides a clear understanding of dialog.
From this figure, we can see that dialog is the parent class and has the most important alertdilog, which is the most commonly used subclass. alertdialog is composed of datpicker, progressdialog, and timepick.
These sub-classes are the most commonly used in program development. Therefore, we can try to figure out the answers to the following questions:
<1> how to generate a dialog?
<2> what is the relationship between dialog and activity? Why does the activity lose focus and cannot be clicked when the dialog is displayed? How does the system manage their relationships?
Taking progressdialog as an example, we can thoroughly understand its internal operating mechanism by analyzing its source code.
<1> how to generate a progressdialog?
By opening the source code of progressdialog, we can see that it has two constructor methods,
Progressdialog (context)
Progressdialog (context, int theme)
Parameter description: Context: it indicates that it relies on context and must be created through a context. The second parameter is theme: the topic of this dialog.
In addition, we can also find four static show methods, which only have different parameters,
Show (context, charsequence title, charsequence message );
Show (context, charsequence title, charsequence message, Boolean indeterminate );
At that time, we only need to look at the last static method. Because of the three static methods, the show static method with the most last parameter will be called to complete the function.
It implements the following functions:
Progressdialog dialog = new progressdiaog (context );
...
...
...
Dialog. Show ();
This is actually the same as calling the constructor.
At this step, I only know how to call the progressdialog generated, but the specific internal details. Also look at the oncreate () method. This method is the place where progressdialogr can be generated. It is the same as the oncreate () method in the activity, which is called during initialization. The source code is as follows:
The first thing we can see is a handler named mviewupdatehandler. This handler is mainly used to update the progress value (percentage), and then let's look at it...
View view = Inflater. Inflate (R. layout. alert_dialog_press, null );
Obviously, this is what we see, the stuff shown in the dialog circle. however, the alert_dialog_pross file is in COM. android. internal. the r package is a system-defined UI configuration file.
Finally, call the setview (View) method to display the view on the dialog.
Therefore, we can also customize a dialog. xml file to call the setview () method for display.
This setview () method is defined in the alertdialog class and will be explained later!
This is what we usually see in progressdialog. The analysis is complete.
Alertdialog
Next, we will analyze the parent class alertdialog of progressdialog.
When you open the source code, three constructor methods are displayed, but all three constructor methods are of the protected type,
It can be seen that alertdialog cannot be instantiated directly. Therefore, let's see if there are other methods.
Take a closer look and find a variable,
Alertcontroller malert; this is the main character of today, focusing on it.
Malert is defined as the constructor of alertdialog,
In addition, we also found that almost all methods in alertdialog are operated through this malert variable,
That is to say, alertdialog is an empty shell and has no practical effect. Its essence is the alertcontroller class.
It is the "main character" I just mentioned ". The source code of alertcontroller is provided by the Android system. We need to make some effort to find it.
Looking down, a static class builder is found in the alertdialog class. Through this class, we can also directly create a dialog,
The builder static class also has an alertcontroller. alertparams Class Object P. Let's look at the builder source code, which can set all the dialog methods, including
Settitle (charsequence title)
Setmessage (INT messageid );
Seticon (INT iconid );
Setpositivebutton ()
...
...
....
Wait, we can see that the internal implementation of these methods is all implemented for its built-in object P, that is, the methods we call. The final result is actually used in alertcontroller. object P of the alertparams class,
In addition, there is an interesting phenomenon. The return values of these methods are builder objects. That is to say, we can call this method consecutively,
However, it is not enough to display a dialog and only call these set methods. The last step is to call the CREATE () method, which is the last call, that is, to create a dialog box, we can see that all the actions are done in the create method.
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 (). We can intuitively see that it helped us create an alertdialog object,
Here, p is the object of alertcontroller. alertparams we created,
In alertdialog dialog = new alertdialog (P. mcontext); in this Code, the dialog is defined and the alertcontroller object malert is also defined;
In the second code, P. apply (dialog. malert), let's track the past and look at it. Haha, it turns out that the value of the set method we just called is restored to the malert object in the newly defined dialog. Now we understand, it turns out that P is just a "fart", a temporary data storage object. As long as we call the create method, P will hand over the data to the dialog object,
In addition to the create method, there is also a show () method, which is implemented as follows:
Public alertdialog show (){
Alertdialog dialog = create ();
Dialog. Show ();
Return dialog;
}
We can see that show () is also the call of the create method.
From the following analysis, we may easily understand the following common statements,
Return new alertdialog. Builder (context ).
Seticon (R. drawable. alert_icon ).
Settitle (R. String. alert_str)
. Create ();
This is what we usually use to generate a dialog