This example describes the Android Dialog dialog box usage. Share to everyone for your reference, specific as follows:
Activities provides an easy to manage dialog box mechanism for creating, saving, and replying, such as Oncreatedialog (int), onpreparedialog (int, Dialog), ShowDialog (int), Methods such as DismissDialog (int), which, if used, will return the activity-Managed dialog box (dialog) through the Getowneractivity () method.
1. Oncreatedialog (int): When you use this callback function, the Android system effectively sets up the activity as the owner of each dialog box, automatically manages the status of each dialog box and is anchored to the activity. In this way, each dialog box inherits the specific attributes of the activity. For example, when a dialog box is opened, the menu key displays the menu of options defined for this activity, and the volume key modifies the audio stream used by the activity.
2. ShowDialog (int): When you want to display a dialog box, call the ShowDialog (int id) method and pass a unique integer that identifies the dialog box. When the dialog box is first requested, Android calls Oncreatedialog (int id) from your activity, where you should initialize the dialog dialog. This callback method is passed with the same ID as the ShowDialog (int id). When you create this dialog box, return the object at the end of the activity.
3. Onpreparedialog (int, Dialog): Android also invokes the optional callback function onpreparedialog (int ID, Dialog) before the dialog box is displayed. You can define this method if you want to change any of its properties when each dialog box is opened. This method is invoked each time the dialog box is opened, and Oncreatedialog (int) is invoked only the first time the dialog box is opened. If you do not define Onpreparedialog (), the dialog box will remain the same as it was last opened. This method is also passed with the ID of the dialog box and the dialog object created in Oncreatedialog ().
4. DismissDialog (int): When you are ready to close the dialog box, you can eliminate it by calling dismiss () on the dialog. If you want, you can also call the dismissdialog (int id) method from this activity, which will actually call the dismiss () method for you on this dialog box. If you want to use the Oncreatedialog (int id) method to manage the state of your dialog box (as discussed in the previous section), and each time your dialog box is eliminated, the state of the dialog object will be retained by the activity. If you decide that you no longer need this object or that it is important to clear the state, then you should call Removedialog (int id). This will remove any internal object references and, if the dialog box is being displayed, it will be eliminated.
Use one: When you press the return button, pop up a prompt to ensure that the operation is correct, using a common dialog box style
protected void Dialog () {
Alertdialog.builder Builder = new Builder (main.this);
Builder.setmessage ("Confirm exit"?) ");
Builder.settitle ("hint");
Builder.setpositivebutton ("Confirm", new Onclicklistener () {
@Override public
void OnClick (Dialoginterface dialog , int which) {
Dialog.dismiss ();
Main.this.finish ();
}
);
Builder.setnegativebutton ("Cancel", new Onclicklistener () {
@Override public
void OnClick (dialoginterface dialog, int which) {
Dialog.dismiss ();
}
});
Builder.create (). Show ();
This method is called in the onkeydown (int keycode, KeyEvent event) method
public boolean onKeyDown (int keycode, keyevent event) {
if (keycode = = Keyevent.keycode_back && event.getrep Eatcount () = = 0) {
dialog ();
}
return false;
}
Use method Two: Change the dialog box chart, added three buttons
Dialog Dialog = new Alertdialog.builder (this). SetIcon (Android. R.drawable.btn_star). Settitle ("Hobby Survey"). Setmessage ("Do you like Jet Li's movies?"). "). Setpositivebutton (" Very like ", New Onclicklistener () {@Override public void OnClick (Dialoginterface dial OG, int which) {//TODO auto-generated Method Stub toast.maketext (Main.this, "I like his movies very much."
", Toast.length_long). Show (); }). Setnegativebutton ("Dislike", new Onclicklistener () {@Override public void OnClick (Dialoginter Face dialog, int which) {//TODO auto-generated Method Stub toast.maketext (Main.this, "I don't like his movies."
", Toast.length_long). Show (); }). Setneutralbutton ("General", new Onclicklistener () {@Override public void OnClick (Dialoginterf Ace dialog, int which) {//TODO auto-generated Method Stub toast.maketext (Main.this, "not to be liked."
", Toast.length_long). Show ();
). Create (); Dialog.show();
How to use three: information content is a simple view type
New Alertdialog.builder (This)
. Settitle ("Please enter")
. SetIcon (Android. R.drawable.ic_dialog_info)
. Setview (New EditText (This)).
Setpositivebutton ("OK", null)
. Setnegativebutton ("Cancel", null)
. Show ();
How to use four: information content is a group of radio boxes
New Alertdialog.builder (This)
. Settitle ("check box")
. Setmultichoiceitems (New string[] {"Item1", "Item2"}, NULL, NULL)
. Setpositivebutton (OK, null)
. Setnegativebutton ("Cancel", null). Show
();
Use method Five: Information content is a set of multiple-selection boxes
New Alertdialog.builder (This)
. Settitle ("Radio Box")
. SetIcon (Android. R.drawable.ic_dialog_info)
. Setsinglechoiceitems (New string[] {"Item1", "Item2"}, 0,
New Dialoginterface.onclicklistener () {public
void OnClick (dialoginterface dialog, int which) {
Dialog.dismiss () ;
}
})
. Setnegativebutton ("Cancel", null). Show ();
Using method Six: The information content is a set of simple list items
New Alertdialog.builder (This)
. Settitle ("ListBox")
. Setitems (New string[] {"Item1", "Item2"}, NULL).
Setnegativebutton (OK, null). Show
();
How to use Seven: information content is a custom layout
Layout file:
<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android=
"http://schemas.android.com/apk/" Res/android "
android:id=" @+id/dialog "
android:layout_width=" wrap_content "
android:layout_height=" Wrap_content "
android:background=" "#ffffffff" android:orientation= "horizontal"
>
<textview
android:id= "@+id/tvname"
android:layout_width= "wrap_content"
android:layout_height= "Wrap_content" "
android:text=" name: "/>
<edittext
android:id=" @+id/etname "android:layout_width=" Wrap
_content "
android:layout_height=" wrap_content "
android:minwidth=" 100dip "/>
</linearlayout >
Calling code:
Layoutinflater inflater = Getlayoutinflater ();
View layout = Inflater.inflate (R.layout.dialog, (ViewGroup) Findviewbyid (R.id.dialog));
New Alertdialog.builder (This)
. Settitle ("Custom Layout")
. Setview (Layout).
Setpositivebutton (OK, null)
. Setnegativebutton ("Cancel", null)
. Show ();
Use method Seven: fragment, show only the confirmation button
New Alertdialog.builder (Getactivity ())
. Setmessage (M)
. Setpositivebutton ("OK",
new Dialoginterface.onclicklistener () {public
void OnClick (dialoginterface dialoginterface, int i) {}
})
. Show ();
For more information on Android-related content readers can view the site: "The activity of Android programming skills Summary", "Android Resources Operating Skills Summary", "Android File Operating skills summary", " Android Operation SQLite Database skills Summary, "Android operation JSON format Data Skills summary", "Android Database Operation skills Summary", "Android programming development of SD card Operation Summary", "Android Development introduction and Advanced Course", The Android View view tips summary and the Android Control usage summary
I hope this article will help you with the Android program.