Android dialog box Introduction

Source: Internet
Author: User

There are four types of dialog boxes in Android: General Dialog Box form, List dialog box form, single-choice button dialog box, and multi-choice button dialog box. I will explain them one by one.

<1> General dialog box

The general dialog box format is as follows:

The specific implementation code is as follows:

1 New alertdialog. Builder (this) 2. settitle ("delete") // set the Title 3. setmessage ("are you sure you want to delete the specified record? ") // Set the prompt Message 4. setpositivebutton ("OK", new dialoginterface. onclicklistener () {// set the OK button 5 @ override 6 Public void onclick (dialoginterface dialog, int which) {7 // Do Something 8} 9}) 10. setnegativebutton ("cancel", new dialoginterface. onclicklistener () {// set cancel key 11 @ override12 public void onclick (dialoginterface dialog, int which) {13 // do something14} 15}) 16. setneutralbutton ("Ignore", new dialoginterface. onclicklistener () {// neutral, nothing good 17 @ override18 public void onclick (dialoginterface dialog, int which) {19 // do something20} 21}) 22. setcancelable (false) // set whether to return the response by pressing the return key. This is not a response. 23. show (); // display

When each button is set, the parameter (charsequence text, onclicklistener listener) is passed. Text is the text of the button, and listener is a dialoginterface. onclicklistener (), so we need to create this object and implement its click events. The clicking event contains a which parameter, which is actually a unique int value. As the which in setpositivebutton above indicates-1, the which in setnegativebutton indicates-3, and the which in setneutralbutton indicates-2.

Therefore, to simplify the code, we will not create a dialoginterface. onclicklistener () object for each button, but create only one dialoginterface. onclicklistener () object to implement their click events separately. The specific implementation code is as follows:

1 dialoginterface. onclicklistener dialog = new dialoginterface. onclicklistener () {2 Public void onclick (dialoginterface diich, int which) {3 if (which = dialoginterface. button_positive) {// Click Event 4 toast. maketext (mainactivity. this, "OK! ", 1 ). show (); 5} 6 else if (which = dialoginterface. button_negative) {// Click Event 7 toast to cancel the button. maketext (mainactivity. this, "cancel", 1 ). show (); 8} 9 else if (which = dialoginterface. button_neutral) {// ignore the click event 10 toast. maketext (mainactivity. this, "do not know", 1 ). show (); 11} 12} 13 };
1 // Dialog Box 2 new alertdialog. Builder (this) 3. settitle ("delete") // set the Title 4. setmessage ("are you sure you want to delete the specified record? ") // Set the prompt message. setpositivebutton ("OK", DIALOG) 6. setnegativebutton ("cancel", DIALOG) 7. setneutralbutton ("Ignore", DIALOG) 8. setcancelable (false) // set whether to return the response by pressing the return key. This is not a response. show (); // display

<2> List dialog box

After understanding the implementation of the preceding general dialog box, the following three dialogs can be easily implemented. The implementation effects of the List dialog box are as follows:

The specific implementation code is as follows:

1 final string [] names = {"Yao Ming", "Kobe", "James"}; // an array consisting of the content displayed in the List 2 new alertdialog. builder (this) 3. settitle ("List dialog box") // dialog box Title 4. setitems (names, new dialoginterface. onclicklistener () {// name of each entry 5 Public void onclick (dialoginterface dialog, int which) {// response to click event 6 toast. maketext (mainactivity. this, Names [which], 1 ). show (); 7} 8}) 9. setpositivebutton ("OK", new dialoginterface. onclicklistener () {// response confirmation key click event 10 11 @ override12 public void onclick (dialoginterface dialog, int which) {13 // do something14} 15}) 16. show ();

Setitems is used when setting the list display. setitems has parameters such as (charsequence [] items, onclicklistener listener) in setitems, the first parameter is the array of the names of each entry in the list display, and the second parameter is the response event when you click a list to display the entry, therefore, we can define an array to store the name of the list to be displayed. This makes it much easier to use. We need to implement the onclick event when the conditional click affects the event. Here there is also a parameter which, which is interpretedWhichThe
Button that was clicked (e.g.DialogInterface.BUTTON1) Or the position of the item clicked. It is the same as the which above and also represents a unique value. It represents the sub-value of the array that stores the list name defined by ourselves. Therefore, we can access the values in the array by subscript. Of course, we can also add a button in the List dialog box. This is also possible. Of course, we can also perform a response event for this button. I will not talk about it here.

<3> single choice button dialog box

The Running Effect of the single-choice button dialog box is as follows:

The specific implementation code is as follows:

1 final string [] lang = {"NBA", "Premier League", "La Liga"}; 2 new alertdialog. builder (this) 3. settitle ("single choice button dialog box") // Title 4. setsinglechoiceitems (Lang, 0, new dialoginterface. onclicklistener () {// set entry 5 Public void onclick (dialoginterface dialog, int which) {// RESPONSE event 6 // do something 7 // close Dialog Box 8 dihing. dismiss (); 9} 10}) 11. setpositivebutton ("OK", new dialoginterface. onclicklistener () {12 13 @ override14 public void onclick (dialoginterface dialog, int which) {15 // do something16} 17}) // Add a button and respond to the Click Event 18. show ();

Setsinglechoiceitems is used when the set list is displayed, which is included in setsinglechoiceitems (charsequence []
Items, int checkeditem, onclicklistener listener)
The first parameter is an array of the names of each entry displayed in the list, and the second parameter is an array of the entries selected by default, I set the first entry to be selected by default, so I will write the first entry in the array with the value 0 in the checkeditem parameter. If I have not been selected by default, I will write-1 here, the third is the response event when you click a list to display entries. Therefore, we can define an array to store the name of the list to be displayed. This makes it much easier to use. We need to implement the onclick event when the conditional click affects the event. Here there is also a parameter which, which is interpretedWhichThe
Button that was clicked (e.g.DialogInterface.BUTTON1) Or the position of the item clicked. It is the same as the which above and also represents a unique value. It represents the sub-value of the array that stores the list name defined by ourselves. Therefore, we can access the values in the array by subscript. After clicking it, we can set it to make the dialog box disappear. here we can use dialog. Dismiss (); to make the dialog box disappear. Of course, we can also add a button in the List dialog box. This is also possible. Of course, we can also perform a response event for this button. I will not talk about it here.

<4> multi-choice dialog box

The Running Effect of the multiple-choice button dialog box is as follows:

1 final string [] Langs = {"NBA", "La Liga", "Premier League"}; 2 Final Boolean [] selected = new Boolean [] {true, false, true }; // an array containing boolean values 3 new alertdialog. builder (this) 4. settitle ("Multi-choice List dialog box") 5 // Title 6. setmultichoiceitems (Langs, selected, 7 new dialoginterface. onmultichoiceclicklistener () {// set multiple-choice entry 8 Public void onclick (dialoginterface dialog, int which, Boolean ischecked) {9 // do something10} 11}) 12. setpositivebutton ("OK", 13 new dialoginterface. onclicklistener () {14 public void onclick (dialoginterface dialog, 15 int which) {16 // do something17} 18 }). show ();

In the multi-choice button dialog box, we use setmultichoiceitems to set the multi-choice button. Here, the parameter in setmultichoiceitems is (charsequence []
Items, Boolean [] checkeditems, onmultichoiceclicklistener listener), the first parameter is the array of the name of the entry to be displayed. Here we store it using a string array, the second is a Boolean array of which buttons are selected by default. Here, I will select the entries whose subscript is 0 and 2 by default. Of course, we can also define a Boolean array in advance. The third parameter is the response event when you click the multiple-choice button. In this onclick event, there are also three parameters (dialoginterfacedialog,
Int which, Boolean ischecked), the first parameter represents this dialog box, and the second parameter represents the value of the value stored in the string array. The third parameter represents the Boolean value in the Boolean array. Of course, we can also add a button in the List dialog box. This is also possible. Of course, we can also perform a response event for this button. I will not talk about it here.

The above are four dialog box formats in Android. When using them, we must learn to use them flexibly. Instead of limiting them to a specific format, we can design our own dialog box. Do not be bound by thoughts.

Reprinted from http://www.cnblogs.com/zxl-jay/archive/2011/09/30/2196671.html

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.