Common android pop-up dialog box

Source: Internet
Author: User

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
If we want to implement a dialog box with specific requirements, we may first think of rewriting our dialog box through inheritance and other methods. Of course, this is also a good solution, but in general
In this case, the rewrote dialog box may only be used in a specific place. To create a new class for this use, it is often a bit of a cool-killing feeling, it is not even necessary to add programs.
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.
In-depth understanding of the document, found that android has provided the corresponding interface dialog Builder
Next, let me share the relevant content here, so that more beginners can avoid detours.

The first is a simple application, that is, a message box is displayed, which can be implemented in Android.

View plain
Copy to clipboard
Print
?


 

View plain
Copy to clipboard
Print
?
  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 ();

1 <br/> New alertdialog. builder (Self) <br/> 2 <br/>. settitle ("title") <br/> 3 <br/>. setmessage ("simple message box") <br/> 4 <br/>. setpositivebutton ("OK", null) <br/> 5 <br/>. show ();

The effect is as follows:


 

Upper
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 is displayed. Note the parameters of the builder method.
SELF: This is actually a reference to the activity object. You can import the reference based on your context. For example, to call the oncreate method, you only need to input this
Yes.

The following is a dialog box with confirmation and cancellation buttons

  1. View plain
    Copy to clipboard
    Print
    ?
    1. New
      Alertdialog. Builder (Self)
    2. . Settitle ("OK"
      )
    3. . Setmessage ("OK? "
      )
    4. . Setpositivebutton ("yes"
      ,
      Null
      )
    5. . Setnegativebutton ("no"
      ,
      Null
      )
    6. . Show ();

    New alertdialog. Builder (Self) <br/>. settitle ("OK") <br/>. setmessage ("OK? ") <Br/>. setpositivebutton (" yes ", null) <br/>. setnegativebutton (" no ", null) <br/>. Show ();
     

Copy code



 

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.

The following is a dialog box that allows you to enter text.

  1. View plain
    Copy to clipboard
    Print
    ?
    1. New
      Alertdialog. Builder (Self)
    2. . Settitle ("enter"
      )
    3. . Seticon (Android. R. drawable. ic_dialog_info)
    4. . Setview (New
      Edittext (Self ))
    5. . Setpositivebutton ("OK"
      ,
      Null
      )
    6. . Setnegativebutton ("cancel"
      ,
      Null
      )
    7. . Show ();

    New alertdialog. builder (Self) <br/>. settitle ("enter") <br/>. seticon (Android. r. drawable. ic_dialog_info) <br/>. setview (New edittext (Self) <br/>. setpositivebutton ("OK", null) <br/>. setnegativebutton ("cancel", null) <br/>. 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

The following are single-choice and multi-choice boxes, which are also very useful dialog boxes.

  1. View plain
    Copy to clipboard
    Print
    ?
    1. New
      Alertdialog. Builder (Self)
    2. . Settitle ("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 ();

    New alertdialog. builder (Self) <br/>. settitle ("select") <br/>. seticon (Android. r. drawable. ic_dialog_info) <br/>. setsinglechoiceitems (New String [] {"option 1", "option 2", "option 3", "option 4"}, 0, <br/> New dialoginterface. onclicklistener () {</P> <p> Public void onclick (dialoginterface dialog, int which) {<br/> dialog. dismiss (); <br/>}< br/>) <br/>. setnegativebutton ("cancel", null) <br/>. show ();
     





 


  1. View plain
    Copy to clipboard
    Print
    ?

    1. New
      Alertdialog. Builder (Self)
    2. . Settitle ("Multi-choice box"
      )
    3. . Setmultichoiceitems (New
      String [] {
      "Option 1"
      ,
      "Option 2"
      ,
      "Option 3"
      ,
      "Option 4"
      },
      Null
      ,
      Null
      )
    4. . Setpositivebutton ("OK"
      ,
      Null
      )
    5. . Setnegativebutton ("cancel"
      ,
      Null
      )
    6. . Show ();

    <Br/> New alertdialog. builder (Self) <br/>. settitle ("multiple choice box") <br/>. setmultichoiceitems (New String [] {"option 1", "option 2", "option 3", "option 4"}, null, null) <br/>. setpositivebutton ("OK", null) <br/>. setnegativebutton ("cancel", null) <br/>. show ();



 

The single-choice dialog box and multi-choice dialog box should be frequently used, and the code should be well understood. Next we will introduce two,

List dialog box

  1. View plain
    Copy to clipboard
    Print
    ?
    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 ();

    New alertdialog. builder (Self) <br/>. settitle ("list box") <br/>. setitems (New String [] {"list item 1", "list item 2", "list item 3"}, null) <br/>. setnegativebutton ("OK", null) <br/>. show ();
     



 

Finally, the image is displayed in the dialog box.

  1. View plain
    Copy to clipboard
    Print
    ?
    1. Imageview IMG =
      New
      Imageview (Self );
    2. IMG. setimageresource (R. drawable. Icon );
    3. New
      Alertdialog. Builder (Self)
    4. . Settitle ("image box"
      )
    5. . Setview (IMG)
    6. . Setpositivebutton ("OK"
      ,
      Null
      )
    7. . Show ();

    Imageview IMG = new imageview (Self); <br/> IMG. setimageresource (R. drawable. icon); <br/> New alertdialog. builder (Self) <br/>. settitle ("image box") <br/>. setview (IMG) <br/>. setpositivebutton ("OK", null) <br/>. 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.

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.