Figure 1 Effect: This effect is a prompt that pops up when you press the Back button to ensure error-correct operation, using a common dialog box style.
Code:
Create a dialog box Method dialog ()
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.getrepeatcount () = = 0) {
Dialog ();
}
return false;
}
Figure 2 Effect: Change the Chart of the dialog box, add three buttons
Dialog Dialog = new Alertdialog.builder (this). SetIcon (
Android. R.drawable.btn_star). Settitle ("Survey of Preferences"). Setmessage (
"Do you like Jet Li's movies?" "). Setpositivebutton (" Very like ",
New Onclicklistener () {
@Override
public void OnClick (Dialoginterface dialog, int which) {
TODO auto-generated Method Stub
Toast.maketext (Main.this, "I like his movie very much. ",
Toast.length_long). Show ();
}
}). Setnegativebutton ("Don't like", New Onclicklistener () {
@Override
public void OnClick (Dialoginterface 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 (Dialoginterface dialog, int which) {
TODO auto-generated Method Stub
Toast.maketext (Main.this, "I don't like it. ", Toast.length_long)
. Show ();
}
}). Create ();
Dialog.show ();
Figure 3 Effect: The 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 ();
Figure 4 Effect: information content is a set of radio boxes
New Alertdialog.builder (this). Settitle ("check box"). Setmultichoiceitems (
New string[] {"Item1", "Item2"}, NULL, NULL)
. Setpositivebutton ("OK", NULL)
. Setnegativebutton ("Cancel", null). Show ();
Figure 5 Effect: information content is a set of multi-marquee
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 ();
Figure 6 Effect: information content is a set of simple list items
New Alertdialog.builder (this). Settitle ("list box"). Setitems (
New string[] {"Item1", "Item2"}, NULL). Setnegativebutton (
OK, null). Show ();
Figure 7 Effect: information content is a customized layout
1. Layout files
<?xml version= "1.0" encoding= "Utf-8"?>
<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
android:layout_height= "Wrap_content" android:layout_width= "Wrap_content"
Android:background= "#ffffffff" android:orientation= "Horizontal"
Android:id= "@+id/dialog" >
<textview android:layout_height= "Wrap_content"
Android:layout_width= "Wrap_content"
Android:id= "@+id/tvname" android:text= "Name:"/>
<edittext android:layout_height= "Wrap_content"
Android:layout_width= "Wrap_content" android:id= "@+id/etname" android:minwidth= "100dip"/>
</LinearLayout>
2. 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 ();
All kinds of dialog