Android Development Alertdialog Parsing

Source: Internet
Author: User

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.

Http://www.cnblogs.com/jqyp/archive/2011/06/17/2083533.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.