Go to Android dialog box (Dialog) To build your own dialog

Source: Internet
Author: User

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.

Here are the effects of several dialog boxes

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: Creating a dialog Box Method dialog ()protected voiddialog () {Alertdialog.builder Builder=NewBuilder (Main. This); Builder.setmessage ("Are you sure you want to quit? "); Builder.settitle (Prompted); Builder.setpositivebutton ("Confirm",NewOnclicklistener () {@Override Public voidOnClick (Dialoginterface Dialog,intwhich)    {Dialog.dismiss (); Main. This. Finish ();  }  }); Builder.setnegativebutton ("Cancel",NewOnclicklistener () {@Override Public voidOnClick (Dialoginterface Dialog,intwhich)   {Dialog.dismiss ();  }  }); Builder.create (). Show (); } in OnKeyDown (intThis method is called in the KeyCode, KeyEvent event) method Public BooleanOnKeyDown (intKeyCode, KeyEvent event) {  if(KeyCode = = Keyevent.keycode_back && event.getrepeatcount () = = 0) {dialog (); }  return false; }

Figure 2

Figure 2 Effect: Change the Chart of the dialog box, add three Buttons Dialog dialog=NewAlertdialog.builder ( This). SetIcon (Android. R.drawable.btn_star). Settitle ("Preferences Survey"). Setmessage ("Do you like Jet Li's movies?" "). Setpositivebutton (" very like ",     NewOnclicklistener () {@Override Public voidOnClick (Dialoginterface Dialog,intwhich) {       //TODO auto-generated Method StubToast.maketext (Main. This, "I like his movie very much. ", Toast.length_long). Show (); }}). Setnegativebutton ("Don't like",NewOnclicklistener () {@Override Public voidOnClick (Dialoginterface Dialog,intwhich) {     //TODO auto-generated Method StubToast.maketext (Main. This, "I don't like his films. ", Toast.length_long). Show (); }}). Setneutralbutton ("General",NewOnclicklistener () {@Override Public voidOnClick (Dialoginterface Dialog,intwhich) {     //TODO auto-generated Method StubToast.maketext (Main. This, "I don't like it. ", Toast.length_long). Show ();   }}). Create (); Dialog.show ();

Figure 3

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 (thisnull)     . Setnegativebutton (null). Show ();

Figure 4

Figure 4 Effect: Information content is a set of radio boxes New Alertdialog.builder (this). Settitle ("check box"). Setmultichoiceitems (     newnull NULL )     . Setpositivebutton (null).     Setnegativebutton (null). Show ();

Figure 5

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 () {      publicvoidint  which) {       Dialog.dismiss ();      }     }). Setnegativebutton (null). Show ();

Figure 6

Figure 6 Effect: Information content is a set of simple list items New Alertdialog.builder (this). Settitle ("list box"). Setitems (     newnull). Setnegativebutton (     null). Show ();

Figure 7

Figure 7 Effect: Information content is a customized layout1. 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)); NewAlertdialog.builder ( This). Settitle ("Custom Layout"). Setview (Layout). Setpositivebutton ("OK",NULL). Setnegativebutton ("Cancel",NULL). Show ();

Go to Android dialog box (Dialog) To build your own dialog

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.