Android dialog box Alertdialog.builder use method _android

Source: Internet
Author: User
Tags inheritance

When we do development in peacetime, inevitably use a variety of dialog boxes, I believe that there are other platform development experience friends will know that most of the platform only provides a few of the simplest implementation, if we want to achieve their specific needs of the dialog box, you may first think, through inheritance and other means, Rewrite our own dialog box. Of course, this is a good way to solve the problem, but the general situation is this, we rewrite the dialog box, perhaps only in a particular place will be used, for this time to use, and to create a new class, often a little overkill feeling, even to our programs add unnecessary complexity, Is there a more elegant solution to the dialog box for this situation?

Luckily, Android offers a solution to this problem, and when I first started to touch Android, I was doing a custom dialog, and it was done by inheritance, and then, as I went deeper into the document, I found that Android startup already provided the appropriate interface dialog Builder, the following I will be related to the content here to share, but also to allow more beginners less detours.

The first is a simple application that pops up a message box that can be implemented in Android

New Alertdialog.builder (self)
settitle ("caption") 
setmessage ("Simple message box")
Setpositivebutton ("OK", null)
Show ();

The effect is as follows:

We created a new alertdialog in the code above and created a chain of objects with the builder method, constructed the dialog we need with a series of setup methods, and then called the Show method to display, noting the arguments to the builder method self, This is actually a reference to the activity object, and it is OK to pass in the appropriate reference according to the context you are in. For example, called in the OnCreate method, just pass in this.
The following is a dialog box with the Confirmation and Cancel buttons:

New Alertdialog.builder (self) 
settitle ("confirm")
setmessage (ok?) ")
Setpositivebutton (" yes ", null)
Setnegativebutton (" No ", null) show
();

Note that there are two null parameters, here is actually the two button click on the Listener, because we do not need to listen to these actions, so the incoming null value is simply ignored, but the actual development of the general need to pass the listener, in response to the user's actions.
Here is a dialog box that you can enter text in:

New Alertdialog.builder (self)
settitle ("Please enter")
SetIcon (Android. R.drawable.ic_dialog_info)
Setview (new EditText (self))
Setpositivebutton (OK, null)
Setnegativebutton ("Cancel", null) show
();


As on the code, we use the Setview method, for our dialog box passed a text edit box, of course, you can pass in any view object, such as picture frame, webview, etc... Play with your imagination.

The following are the two dialog boxes that are useful for both the radio box and the Multiple-selection box:

New Alertdialog.builder (self)
settitle ("Please select")
SetIcon (Android. R.drawable.ic_dialog_info) 
Setsinglechoiceitems (new string[] {"Option 1", "Option 2", "option 3", "Option 4"}, 0, 
New Dialoginterface.onclicklistener () {public
void OnClick (dialoginterface dialog, int which) {
Dialog.dismiss () ;
}
}
)
Setnegativebutton ("Cancel", null) show
();

Radio and multiple selection dialog box should be we usually use a lot of, the code should be very good understanding, and then finally introduced the following two
List dialog box:

New Alertdialog.builder (self)
settitle (list box)
Setitems (new string[] {list item 1, list item 2, list item 3}, NULL)
Setnegativebutton ("OK", null) show
();

Finally, display the picture in the dialog box:


ImageView img = new ImageView (self);
Img.setimageresource (R.drawable.icon);
New Alertdialog.builder (self)
settitle ("Picture frame")
Setview (img)
Setpositivebutton ("OK", null)
Show ();

We passed in a imageview to show the picture, here shows a classic Android small green icon ~ ~, of course, here also can be put on the network picture, the specific implementation of the method is not introduced, leave you to practice it ~

Finally, the Android platform for our development has provided a great convenience, Dialogbuilder can do more than this, here to show you just the tip of the iceberg, we can enjoy the imagination, create our own dialog box.

Related Article

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.