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 2

Figure 3

Figure 4

Figure 5

Figure 6

Figure 7

  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 ();

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.