Figure 1
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:
1 //Create a dialog box Method dialog ()2 3 protected voiddialog () {4Alertdialog.builder Builder =NewBuilder (Main. This);5Builder.setmessage ("Confirm exit?") ");6 7Builder.settitle ("Hint");8 9Builder.setpositivebutton ("Confirm",NewOnclicklistener () {Ten One @Override A Public voidOnClick (Dialoginterface Dialog,intwhich) { - Dialog.dismiss (); - theMain. This. Finish (); - } - }); - +Builder.setnegativebutton ("Cancel",NewOnclicklistener () { - + @Override A Public voidOnClick (Dialoginterface Dialog,intwhich) { at Dialog.dismiss (); - } - }); - - builder.create (). Show (); - } in - to + //This method is called in the onkeydown (int keycode, KeyEvent event) method - the Public BooleanOnKeyDown (intKeyCode, KeyEvent event) { * if(KeyCode = = Keyevent.keycode_back && event.getrepeatcount () = = 0) { $ dialog ();Panax Notoginseng } - return false; the}
Figure 2
1 //Figure 2 Effect: Change the Chart of the dialog box, add three buttons2 3Dialog Dialog =NewAlertdialog.builder ( This). SetIcon (4Android. R.drawable.btn_star). Settitle ("Survey of Preferences"). Setmessage (5"Do you like Jet Li's movies?" "). Setpositivebutton (" very like ",6 NewOnclicklistener () {7 8 @Override9 Public voidOnClick (Dialoginterface Dialog,intwhich) {Ten //TODO auto-generated Method Stub OneToast.maketext (Main. This, "I like his movie very much. ", A Toast.length_long). Show (); - } -}). Setnegativebutton ("Don't like",NewOnclicklistener () { the - @Override - Public voidOnClick (Dialoginterface Dialog,intwhich) { - //TODO auto-generated Method Stub +Toast.maketext (Main. This, "I don't like his films. ", Toast.length_long) - . Show (); + } A}). Setneutralbutton ("General",NewOnclicklistener () { at - @Override - Public voidOnClick (Dialoginterface Dialog,intwhich) { - //TODO auto-generated Method Stub -Toast.maketext (Main. This, "I don't like it. ", Toast.length_long) - . Show (); in } - }). Create (); to +Dialog.show ();
Figure 3
1 // Figure 3 Effect: The information content is a simple view type 2 3 New Alertdialog.builder (this). Settitle ("Please enter"). SetIcon (4 Android. R.drawable.ic_dialog_info). Setview (5 new EditText (thisnull ).6 null). Show ();
Figure 4
1 // Figure 4 Effect: Information content is a set of radio boxes 2 3 New Alertdialog.builder (this). Settitle ("check box"). Setmultichoiceitems (4 new NULL NULL )5 null)6 null). Show ();
Figure 5
1 //Figure 5 Effect: Information content is a set of multi-marquee2 3 NewAlertdialog.builder ( This). Settitle ("Radio Box"). SetIcon (4 Android. R.drawable.ic_dialog_info). Setsinglechoiceitems (5 NewString[] {"Item1", "Item2"}, 0,6 NewDialoginterface.onclicklistener () {7 Public voidOnClick (Dialoginterface Dialog,intwhich) {8 Dialog.dismiss ();9 }Ten}). Setnegativebutton ("Cancel",NULL). Show ();
Figure 6
1 // Figure 6 Effect: Information content is a set of simple list items 2 3 New Alertdialog.builder (this). Settitle ("list box"). Setitems (4 newnull ). Setnegativebutton (5 null). Show ();
Figure 7
1 //Figure 7 Effect: Information content is a customized layout2 31. layout Files4 5<?xml version= "1.0" encoding= "Utf-8"?>6<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"7android:layout_height= "Wrap_content" android:layout_width= "Wrap_content"8Android:background= "#ffffffff" android:orientation= "Horizontal"9Android:id= "@+id/dialog" >Ten<textview android:layout_height= "Wrap_content" OneAndroid:layout_width= "Wrap_content" AAndroid:id= "@+id/tvname" android:text= "Name:"/> -<edittext android:layout_height= "Wrap_content" -Android:layout_width= "Wrap_content" android:id= "@+id/etname" android:minwidth= "100dip"/> the -</LinearLayout> - - //2. Calling code + -Layoutinflater Inflater =Getlayoutinflater (); +View layout =inflater.inflate (R.layout.dialog, A (ViewGroup) Findviewbyid (R.id.dialog)); at - NewAlertdialog.builder ( This). Settitle ("Custom Layout"). Setview (Layout) -. Setpositivebutton ("OK",NULL) -. Setnegativebutton ("Cancel",NULL). Show ();
Activities provides a convenient and manageable dialog box mechanism for creating, saving, and replying, for example,, onCreateDialog(int)
onPrepareDialog(int, Dialog)
showDialog(int)
dismissDialog(int)等方法,如果使用这些方法的话,Activity将通过getOwnerActivity()方法返回该Activity管理的对话框(dialog).
onCreateDialog(int):当你使用这个回调函数时,Android系统会有效的设置这个Activity为每个对话框的所有者,从而自动管理每个对话框的状态并挂靠到Activity上。这样,每个对话框继承这个Activity的特定属性。比如,当一个对话框打开时,菜单键显示为这个Activity定义的选项菜单,音量键修改Activity使用的音频流。
showDialog(int): 当你想要显示一个对话框时,调用showDialog(int id) 方法并传递一个唯一标识这个对话框的整数。当对话框第一次被请求时,Android从你的Activity中调用onCreateDialog(int id),你应该在这里初始化这个对话框Dialog。这个回调方法被传以和showDialog(int id)相同的ID。当你创建这个对话框后,在Activity的最后返回这个对象。
onPrepareDialog(int, Dialog):在对话框被显示之前,Android还调用了可选的回调函数onPrepareDialog(int id, Dialog). 如果你想在每一次对话框被打开时改变它的任何属性,你可以定义这个方法。这个方法在每次打开对话框时被调用,而onCreateDialog(int) 仅在对话框第一次打开时被调用。如果你不定义onPrepareDialog(),那么这个对话框将保持和上次打开时一样。这个方法也被传递以对话框的ID,和在onCreateDialog()中创建的对话框对象。
dismissDialog(int):
When you are ready to close the dialog box, you can remove it by calling dismiss () on the dialog box. If necessary, you can also invoke the dismissdialog (int id) method from this activity, which will actually call the dismiss () method for you to this dialog box. If you want to use the Oncreatedialog (int id) method to manage the state of your dialog (as discussed in the previous section), then each time your dialog box is cleared, the state of the dialog object will be retained by that activity. If you decide that you no longer need this object or clear the state is important, then you should call Removedialog (int id). This will remove any internal object references and if this dialog box is being displayed, it will be eliminated
Android dialog box (Dialog) Daquan "Go"