In GUI programs, it is sometimes necessary to pop up a dialog box to prompt some information. These dialogs are simpler than a standalone screen and pop up in Android
dialog box differs from the activity that represents a screen, which is typically used for simple functional processing.
The parent class of the dialog box is Android.app.Dialog, which implements the pop-up dialog box by building the class Android.app.AlertDialog, which you can use
Alertdialog.builder and different parameters to build the dialog box. Reference Sample program: Dialog
(Apidemo = app=>dialog)
Source code: Com/example/android/apis/app/alertdialogsamples.java Layout file:
Alert_dialog.xml
Dialog running results of the program:
A different dialog will be launched by tapping the different buttons on the screen (except for the 4th button). Implementation method
is to inherit the Oncreatedialog () function, returning a Dialog type:
@Override
protected Dialog oncreatedialog (int id) {
}
The parameter ID of the Oncreatedialog () function is the identifier for the dialog box, which is called when the dialog box is called ShowDialog ().
Public final void ShowDialog (int id)
The ShowDialog () function also distinguishes the dialog box by its ID. Use the ShowDialog () and Oncreatedialog () functions to unify conversations in the activity
Box.
A dialog box with two buttons and prompt information
The 1th button (OK Cancel dialog with a message) launches a prompt message and a two-button dialog box:
Diagram tip Information and two button dialog boxes
The snippet of the code implementation is as follows:
return new Alertdialog.builder (Alertdialogsamples.this)
. SetIcon (R.drawable.alert_dialog_icon)
. Settitle (R.string.alert_dialog_two_buttons_title)
. Setpositivebutton (R.STRING.ALERT_DIALOG_OK, New Dialoginterface.onclicklistener ()
{public void OnClick (dialoginterface dialog, int whichbutton) {
/* Left button event */
}
})
. Setnegativebutton (R.string.alert_dialog_cancel, New Dialoginterface.onclicklistener ()
{public void OnClick (dialoginterface dialog, int whichbutton) {
/* Right-click event */
}
})
Where Setpositivebutton represents the left button set, Setnegativebutton represents the button on the right of the set, and these two buttons are
OK, but you can set the characters it displays and the post-click Behavior function.
A dialog box with three buttons and prompt information
The 2nd button (OK Cancel dialog with a long message) launches a prompt message and a three-button dialog box:
Diagram tip Information and three button dialog boxes
The snippet of the code implementation is as follows:
return new Alertdialog.builder (Alertdialogsamples.this)
. SetIcon (R.drawable.alert_dialog_icon)
. Settitle (R.STRING.ALERT_DIALOG_TWO_BUTTONS_MSG)
. Setmessage (R.STRING.ALERT_DIALOG_TWO_BUTTONS2_MSG)
. Setpositivebutton (R.STRING.ALERT_DIALOG_OK, New Dialoginterface.onclicklistener ()
{public void OnClick (dialoginterface dialog, int whichbutton) {
/* Left button event */
}
})
. Setneutralbutton (R.string.alert_dialog_something, New Dialoginterface.onclicklistener ()
{public void OnClick (dialoginterface dialog, int whichbutton) {
/* Middle Key event */
}
})
. Setnegativebutton (R.string.alert_dialog_cancel, New Dialoginterface.onclicklistener ()
{public void OnClick (dialoginterface dialog, int whichbutton) {
/* Right-click event */
}
})
This dialog box contains 3 buttons, the main difference from the previous example is that the Setneutralbutton () is used to set the middle button.
List Item dialog box
The 3rd button (List dialog) launches a list item dialog box;
Diagram List Items dialog box
The snippet of the code implementation is as follows:
return new Alertdialog.builder (Alertdialogsamples.this)
. Settitle (R.string.select_dialog)
. Setitems (R.array.select_dialog_items, New Dialoginterface.onclicklistener ()
{public void OnClick (Dialoginterface dialog, int which) {
string[] Items =
Getresources (). Getstringarray (R.array.select_dialog_items);
New Alertdialog.builder (Alertdialogsamples.this)
. Setmessage ("You selected:" + which + "," + Items[which])
. Show ();
}
})
Here, Setitems () is used to set up several different items to get select_dialog_items content from the Res/values/array.xml file.
This section reads as follows:
<string-array name= "Select_dialog_items" >
<item>command one</item>
<item>command two</item>
<item>command three</item>
<item>command four</item>
</string-array>
The Item here also sets the Click function, so when they are clicked, a new dialog box will pop up.
Single Option and Button dialog box
The 5th button (single choice list) launches an Option and Button dialog box;
Chart Options and Buttons dialog box
The snippet of the code implementation is as follows:
return new Alertdialog.builder (Alertdialogsamples.this)
. SetIcon (R.drawable.alert_dialog_icon)
. Settitle (R.string.alert_dialog_single_choice)
. Setsinglechoiceitems (R.ARRAY.SELECT_DIALOG_ITEMS2, 0, New Dialoginterface.onclicklistener ()
{public void OnClick (dialoginterface dialog, int whichbutton) {
}
})
. Setpositivebutton (R.STRING.ALERT_DIALOG_OK, New Dialoginterface.onclicklistener ()
{public void OnClick (dialoginterface dialog, int whichbutton) {
/* Left button event */
}
})
. Setnegativebutton (R.string.alert_dialog_cancel, New Dialoginterface.onclicklistener ()
{public void OnClick (dialoginterface dialog, int whichbutton) {
/* Right-click event */
}
})
This example is a dialog box with a single option, with options in a simpler mode, obtained from the Res/values/array.xml file
The content in SELECT_DIALOG_ITEMS2 as a single-option item. This part of the
The contents are as follows:
<string-array name= "SELECT_DIALOG_ITEMS2" >
<item>Map</item>
<item>Satellite</item>
<item>Traffic</item>
<item>street view</item>
</string-array>
Complex options and Buttons dialog box
The 6th button (Repeat alarm) launches a complex options and Buttons dialog box;
Graph Complex options and Buttons dialog box
The snippet of the code implementation is as follows:
return new Alertdialog.builder (Alertdialogsamples.this)
. SetIcon (R.drawable.ic_popup_reminder)
. Settitle (R.string.alert_dialog_multi_choice)
. Setmultichoiceitems (R.ARRAY.SELECT_DIALOG_ITEMS3,
New Boolean[]{false, True, False, True, False, False, false},
New Dialoginterface.onmultichoiceclicklistener () {
public void OnClick (dialoginterface dialog, int whichbutton,
Boolean isChecked) {
/* Click the response of the check box */
}
})
. Setpositivebutton (R.STRING.ALERT_DIALOG_OK, New Dialoginterface.onclicklistener ()
{public void OnClick (dialoginterface dialog, int whichbutton) {
/* Left button event */
}
})
. Setnegativebutton (R.string.alert_dialog_cancel, New Dialoginterface.onclicklistener ()
{public void OnClick (dialoginterface dialog, int whichbutton) {
/* Right-click event */
}
})
. Create ();
This example is a dialog box with a complex option that takes the contents of the SELECT_DIALOG_ITEMS3 as a single option from the Res/values/array.xml file.
Project:
<string-array name= "Select_dialog_items3" >
<item>every monday</item>
<item>every tuesday</item>
<item>every wednesday</item>
<item>every thursday</item>
<item>every friday</item>
<item>every saturday</item>
<item>every sunday</item>
</string-array>
Key dialog box for text (using layout file)
The 7th button (text Entry dialog) launches a key dialog box containing text.
Dialog the effect of invoking individual dialog boxes in a program:
Key dialog box for figure text
The snippet of the code implementation is as follows:
Layoutinflater factory = Layoutinflater.from (this);
Final View Textentryview = Factory.inflate (R.layout.alert_dialog_text_entry,
NULL);
return new Alertdialog.builder (Alertdialogsamples.this)
. SetIcon (R.drawable.alert_dialog_icon)
. Settitle (R.string.alert_dialog_text_entry)
. Setview (Textentryview)
. Setpositivebutton (R.STRING.ALERT_DIALOG_OK, New Dialoginterface.onclicklistener ()
{public void OnClick (dialoginterface dialog, int whichbutton) {
/* Left button event */
}
})
. Setnegativebutton (R.string.alert_dialog_cancel, New Dialoginterface.onclicklistener ()
{public void OnClick (dialoginterface dialog, int whichbutton) {
/* Right-click event */
}
})
. Create ();
}
Alert_dialog_text_entry.xml is also a layout file that contains 2 text boxes and 2 editable text, which is displayed on the screen
, depending on this mode, you can also use the layout file in the popup dialog box.
Thus, in this dialog box, the corresponding controls are included.
As shown in the above dialog, the dialog box can be set with a title, an icon, a ToolTip, up to 3 buttons, a single option, a complex option, and even
To set a View. The last dialog box is implemented by setting a View that sets the contents of the layout file Alert_dialog_text_entry.xml
In
The class of the dialog box is Android.app.Dialog, which is established by the Android.app.AlertDialog.Builder class and can be set up in many
Set up.
SetIcon () and Settitle (): Used to set the icon and title;
setmessage (): Used to set the prompt information;
Setpositivebutton (), Setneutralbutton (), and Setnegativebutton (): Used to set the left, middle, and right buttons;
Setsinglechoiceitems () and Setmultichoiceitems (): For setting single options and complex options;
Setview (): Used to set the contents of a View as a dialog box.
The return type of the above function is Android.app.AlertDialog.Builder, which is the class itself, so you can use the following method to connect the
The continuation is used to set up more content.
Call the Create () function when the settings are complete to return to the Android.app.AlertDialog class, which represents a dialog box that you can use.
Using the dialog box in Android, you can create a simpler form without activity, and the basic interface can be set directly
Get, through Setview () can get arbitrary content interface.
Android Development Popup dialog box