Android learning-common android pop-up dialog box and android pop-up dialog box

Source: Internet
Author: User

Android learning-common android pop-up dialog box and android pop-up dialog box

When we do development at ordinary times, we will inevitably use a variety of dialogs. I believe that anyone who has experience developing other platforms will know that most of the platforms only provide a few of the simplest implementations, if we want to implement a dialog box that meets our specific needs, we may first think of rewriting our own dialog box through inheritance and other methods. Of course, this is also a good solution, but the general situation is that we rewrite the dialog box, maybe only used in a specific place, for this use, creating a new class is often a bit cool, and even adds unnecessary complexity to our program. Is there a more elegant solution to the dialog box in this case?
Fortunately, android provides a solution to this problem. When I first came into contact with android, I implemented a custom dialog box through inheritance, later, with the in-depth understanding of the document, I found that android has provided the corresponding interface Dialog Builder at the beginning. I will share the relevant content here, it can also reduce detours for more beginners.

1. The simplest message pop-up box

New AlertDialog. Builder (self). setTitle ("title"). setMessage ("simple message box"). setPositiveButton ("OK", null). show ();

 

:
 

In the code above, we created an AlertDialog and formed an object chain using the Builder method. Through a series of setting methods, we constructed the required dialog box and then called the show method to display it, note that the parameter self of the Builder method is actually a reference to the Activity object. You can import the reference according to your context. For example, to call the onCreate method, you only need to input this.

 

2. Dialog Box with confirmation and cancellation buttons

New AlertDialog. Builder (self). setTitle ("OK"). setMessage ("OK? "). SetPositiveButton (" yes ", null). setNegativeButton (" no ", null). show ();

 

:

 

Note that there are two null parameters, which are actually listening programs for clicking these two buttons. because we do not need to listen for these actions, the null value is ignored, however, in actual development, listeners must be input to respond to user operations.

 

3. dialog box that allows you to Enter Text

New AlertDialog. builder (self ). setTitle ("enter "). setIcon (android. r. drawable. ic_dialog_info ). setView (new EditText (self )). setPositiveButton ("OK", null ). setNegativeButton ("cancel", null ). show ();

 

 

In the above Code, we use the setView method to input a text edit box for our dialog box. Of course, you can input any view object, such as the slice box and WebView .. Make full use of your imagination ~ : Lol

 

4. Single-choice dialog box and multi-choice box dialog box

New AlertDialog. builder (self ). setTitle ("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 diich, int which) {dialog. dismiss ();}}). setNegativeButton ("cancel", null ). show ();

 

 

 

New AlertDialog. builder (self ). setTitle ("multiple choice box "). setMultiChoiceItems (new String [] {"option 1", "option 2", "option 3", "option 4"}, null, null ). setPositiveButton ("OK", null ). setNegativeButton ("cancel", null ). show ();

 

 

 

New AlertDialog. builder (self ). setTitle ("multiple choice box "). setMultiChoiceItems (new String [] {"option 1", "option 2", "option 3", "option 4"}, null, null ). setPositiveButton ("OK", null ). setNegativeButton ("cancel", null ). show ();

 

 

 

5. 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 ();

 

 

 

6. display images in the dialog box

ImageView img = new ImageView (self); img. setImageResource (R. drawable. icon); new AlertDialog. builder (self ). setTitle ("image box "). setView (img ). setPositiveButton ("OK", null ). show ();

 

 

 

  We passed in an ImageView to display the image. Here we show a classic android little green person icon ~ ~, Of course, you can also put network images here. The specific implementation method will not be introduced. Let's take a look at it ~ : Lol

Finally, the android platform provides great convenience for our development. What DialogBuilder can do is just the tip of the iceberg. We can give full play to our imagination, create our own dialog box.

 

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.