android dialog box

Source: Internet
Author: User

There are four types of dialogs in Android, namely, General dialog, List dialog, radio button dialog, multi-Select button dialog, below I one by one to explain them.

< a > General dialog box

The General dialog box is in the form of:

The specific implementation code is as follows:

1 new Alertdialog.builder (this)
2. Settitle ("delete")//Set Caption
3. Setmessage ("OK to delete the specified records?") Set a prompt message
4. Setpositivebutton ("OK", new Dialoginterface.onclicklistener () {//Set key for OK
5 @Override
6 public void OnClick (Dialoginterface dialog, int which) {
7//do Something
8}
9})
Setnegativebutton ("Cancel", new Dialoginterface.onclicklistener () {//Set Cancel button
@Override
public void OnClick (Dialoginterface dialog, int which) {
//do something
14}
15})
Setneutralbutton ("Ignore", new Dialoginterface.onclicklistener () {//neutral, what's good
@Override
public void OnClick (Dialoginterface dialog, int which) {
//do something
20}
21})
Setcancelable (FALSE)//sets whether the return key is returned as a response, which is not responding
Show ();//Display

When you set each button button, it will pass (charsequence text, Onclicklistener Listener) parameter, text is the key, Listener is a dialoginterface.onclicklistener (), so we want to create this object and implement its Click event. There is a which parameter in the Click event do not know that everyone has noticed that this which actually represents a unique int type value. The which in Setpositivebutton, like the one above, represents the -3,setneutralbutton in -1,setnegativebutton represented by the-2.

So in order to simplify the code here, we no longer create a dialoginterface.onclicklistener () object for each button, but create only one dialoginterface.onclicklistener () Objects 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 dialog, int which) {
3 if (which = = dialoginterface.button_positive) {//OK key Click event
4 Toast.maketext (mainactivity.this, "ok!", 1). Show ();
5 }
6 Else if (which = = dialoginterface.button_negative) {//Cancel keystrokes for click events
7 Toast.maketext (Mainactivity.this, "Cancel", 1). Show ();
8 }
9 Else if (which = = Dialoginterface.button_neutral) {//Ignore click events for keystrokes
Ten Toast.maketext (Mainactivity.this, "Don't Know", 1). Show ();
One }
-- }
;
1//dialog box
2 New Alertdialog.builder (this)
3 . Settitle ("delete")//Set Caption
4 . Setmessage ("OK to delete the specified records?") Set a prompt message
5 . Setpositivebutton ("OK", Dialog)
6 . Setnegativebutton ("Cancel", Dialog)
7 . Setneutralbutton ("Ignore", Dialog)
8 . Setcancelable (FALSE)//Set Press the return key if the response is returned, which is not responding
9 . Show ();//Display

< two > List dialog box

Understand the implementation of the above General dialog box, then the following three kinds of dialog box is easy to implement. The implementation of the list dialog box is as follows:

The specific implementation code is as follows:

1 Final string[] names = {"Yao", "Kobe", "James"};//array of contents 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 bar
5 public void OnClick (Dialoginterface dialog, int which) {//Response click event
6 Toast.maketext (Mainactivity.this, Names[which], 1). Show ();
7}
8})
9. Setpositivebutton ("OK", new Dialoginterface.onclicklistener () {//Response OK key Click event
10
@Override
public void OnClick (Dialoginterface dialog, int which) {
//do something
14}
15})
Show ();

In the settings list we used the Setitems, in Setitems (charsequence[] items, Onclicklistener listener) Such parameters, where the first parameter is the list of each entry in the name of the array, The second is the response event when clicking on a list to display the entry, so here we can define the good one array beforehand to hold the name of the list to be displayed. This is a lot easier when it's time. We want to implement the OnClick event when the conditional click affects the event, there is also a parameter which, which is interpreted in the API as which the button that is clicked (e.g. DialogInterface.BUTTON1 ) or the position of the item clicked. It is the same as the above which and represents a unique value. What it represents here is the subscript value of the array that holds the list name that you define, so we can access the values in the array by subscript. Of course we can also add a button to the List dialog box, which is also possible, and we can of course respond to this button. I'm not going to say much here.

< three > radio Button dialog box

The Radio Button dialog box works 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 ("Radio 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 Dialog.dismiss ();
9 }
(Ten })
Setpositivebutton ("OK", new Dialoginterface.onclicklistener () {
12
@Override
+ Public void OnClick (dialoginterface dialog, int which) {
//do Something
+ }
)//Add button and respond to click events
Show ();

We used setsinglechoiceitems when setting up the list, there were (charsequence[] items in setsinglechoiceitems, int checkeditem,onclicklistener Listener
Such parameters, where the first parameter is an array of the names of each entry in the list display, and the second is the default to make which entry is selected, as in the above code, I let the first one be selected by default, so I am checkeditem this parameter here to write the first in the array of subscript value 0, If I'm not selected by default, I'll write 1 here, and the third one is to click on a list to display the item's response event, so here we can define the good one array beforehand to hold the list name to display. This is a lot easier when it's time. We want to implement the OnClick event when the conditional click affects the event, there is also a parameter which, which is interpreted in the API as which the button that is clicked (e.g. DialogInterface.BUTTON1 ) or the position of the item clicked. It is the same as the above which and represents a unique value. What it represents here is the subscript value of the array that holds the list name that you define, so we can access the values in the array by subscript. At the end of the click we can set let this dialog box disappear, here can use Dialog.dismiss (); Let the dialog box disappear. Of course we can also add a button to the List dialog box, which is also possible, and we can of course respond to this button. I'm not going to say much here.

< four > Multi-select button dialog box

The Multi-Select Button dialog box works as follows:

1 Final string[] langs = {"NBA", "La Liga", "Premier League"};
2 Final boolean[] selected = new boolean[] {true, false, True};//an array that holds a Boolean value
3 new Alertdialog.builder (this)
4. Settitle ("Multi-select list dialog box")
5//Title
6. Setmultichoiceitems (Langs, selected,
7 new Dialoginterface.onmultichoiceclicklistener () {//Set multiple selection entries
8 public void OnClick (Dialoginterface dialog,int which, Boolean isChecked) {
9//Do something
10}
11})
Setpositivebutton ("OK",
New Dialoginterface.onclicklistener () {
The public void OnClick (Dialoginterface dialog,
int which) {
+//do something
17}
). Show ();

In the Multi-Select Button dialog box, we use Setmultichoiceitems to set the multi-select button, where the parameters in Setmultichoiceitems are (charsequence[] items, boolean[] CheckedItems, Onmultichoiceclicklistener listener), the first parameter is an array of the names of the entries to be displayed, here we use a string array to store, and the second is a Boolean array that defaults which buttons are selected, here, I let the entry labeled 0 and 2 be selected by default, and we can, of course, define the good one Boolean array beforehand. The third parameter is the response event when the multi-select button entry is clicked. In this onclick event, there are also three parameters (Dialoginterfacedialog, int which, Boolean isChecked), and the first parameter represents the dialog box, The second parameter represents the subscript value of the numeric value stored in the string array. The third parameter represents a Boolean value in the Boolean array. Of course we can also add a button to the List dialog box, which is also possible, and we can of course respond to this button. I'm not going to say much here.

These are the four types of dialogs in Android, which we should learn to use flexibly, not confined to a specific form, and we can design our own dialog box. Don't be bound by your thoughts.

2, like the list of this selection of pop-up dialog box, to change the style is generally taken to rewrite layout mode

Today only to realize that you can actually customize the style, to share with you, in fact, very simple

Alertdialog.builder Builder = new Alertdialog.builder (new Contextthemewrapper (this, r.style.alertdialogcustom));

And then you can customize your own style.

<?xml version= "1.0" encoding= "Utf-8"?>

<resources>

<style name= "Alertdialogcustom" parent= "@android: Style/alertdialog" >

<item name= "Android:textcolor" > #00FF00 </item>

<item name= "Android:typeface" >monospace</item>

<item name= "Android:textsize" >10sp</item>

</style>

</resources>

And then the dialog style.

<style name= "dialog" parent= "@android: Style/theme.dialog";
  <item name= "Android: Windowframe "> @null </item>
  <item name=" android:windowisfloating ">true</item>
  <item name= "android:windowistranslucent" >TRUE</ITEM>
  <item name= " Android:windownotitle ">TRUE</ITEM>
  <item name=" Android:background "> @android: Color /transparent</item>
  <item name= "Android:windowbackground" > @android: Color/transparent </item>
  <item name= "android:backgrounddimenabled" >TRUE</ITEM>
   <item name= "Android:backgrounddimamount" >0.6</ITEM>
 </style>

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.