Popular Android Pop-up dialog box

Source: Internet
Author: User

When we do development in peacetime, inevitably will use a variety of dialog boxes, I believe that there are other platform development experience of friends will know that most of the platform is only to provide a few of the simplest implementation, if we want to achieve their own specific needs of the dialog box, you may first think, through inheritance and other ways, Rewrite our own dialog box. Of course, this is a good solution, but the general situation is that, we rewrite the dialog box, perhaps only in a specific place will be used, in order to use this time, and to create a new class, often a little overkill feeling, and even add unnecessary complexity to our program, Is there a more elegant solution to the dialog box for this situation?
Fortunately, Android offers a solution to this problem, and when I first approached Android, I was doing a custom dialog, and it was done in a way that was inherited, and later, with a deeper understanding of the document, it was discovered that the Android starter already provided the appropriate interface dialog Builder, below I will be related to the content here to share, but also to make more beginners less detours.

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

[Java]View Plaincopy

[Java]View Plaincopy
  1. 1
  2. New Alertdialog.builder (self)
  3. 2
  4. . Settitle ("title")
  5. 3
  6. . Setmessage ("Simple message box")
  7. 4
  8. . Setpositivebutton ("OK", null)
  9. 5
  10. . Show ();



The effect is as follows:




In the above code we create a new alertdialog, and use the Builder method to form an object chain, through a series of setup methods, construct the dialog box we need, and then call the Show method to display, notice the builder method parameter self, This is actually a reference to the activity object, and it is possible to pass in the appropriate reference according to the context in which you are located. For example, called in the OnCreate method, just pass in this.


The following is a dialog box with the Confirm and Cancel buttons

    1. [Java] view plaincopy
      1. New Alertdialog.builder (self)
      2. . Settitle ("confirm")
      3. . Setmessage ("OK? ")  
      4. . Setpositivebutton ("yes", null)
      5. . Setnegativebutton ("No", null)
      6. . Show ();
Copy Code




Notice 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 time is generally required to pass the listener, in response to the user's operation.

Here is a dialog box that allows you to enter text

    1. [Java] view plaincopy
      1. New Alertdialog.builder (self)
      2. . Settitle ("Please enter")
      3. . SetIcon (Android. R.drawable.ic_dialog_info)
      4. . Setview (new EditText (self))
      5. . Setpositivebutton ("OK", null)
      6. . Setnegativebutton ("Cancel", null)
      7. . Show ();





As on the code, we use the Setview method, for our dialog box passed a text edit box, of course, you can pass any view object, than the frame, webview and so on. Play with your imagination, ~:lol.

Here are the radio boxes and the multi-box, which are also useful for both dialog boxes

  1. [Java]View Plaincopy
    1. New Alertdialog.builder (self)
    2. . Settitle ("Please select")
    3. . SetIcon (Android. R.drawable.ic_dialog_info)
    4. . Setsinglechoiceitems (new string[] {"option 1","option 2","option 3","Option 4"}, 0,
    5. New Dialoginterface.onclicklistener () {
    6. public void OnClick (Dialoginterface dialog, int which) {
    7. Dialog.dismiss ();
    8. }
    9. }
    10. )
    11. . Setnegativebutton ("Cancel", null)
    12. . Show ();
     





  1. [Java]View Plaincopy
    1. New Alertdialog.builder (self)
    2. . Settitle ("multiple marquee")
    3. . Setmultichoiceitems (new string[] {"option 1","option 2","option 3","Option 4"}, null, null)
    4. . Setpositivebutton ("OK", null)
    5. . Setnegativebutton ("Cancel", null)
    6. . Show ();
                   





Radio and Multi-select dialog should be we usually use a lot of, the code should be very well understood, the following is the final introduction of two,

List dialog box

    1. [Java] view plaincopy
      1. New Alertdialog.builder (self)
      2. . Settitle ("list box")
      3. . Setitems (new string[] {"list item 1","list item 2","list item 3"}, null)
      4. . Setnegativebutton ("OK", null)
      5. . Show ();






Finally, the picture is displayed in the dialog box

    1. [Java] view plaincopy
      1. ImageView img = new ImageView (self);
      2. Img.setimageresource (R.drawable.icon);
      3. New Alertdialog.builder (self)
      4. . Settitle ("Picture frame")
      5. . Setview (IMG)
      6. . Setpositivebutton ("OK", null)
      7. . Show ();




We passed a imageview to display the picture, here shows a classic Android small green man icon ~ ~, of course, here can also put on the network picture, the specific implementation method is not introduced, leave everyone to practice it ~:lol

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

Popular Android Pop-up 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.