Android dialog box (Dialog) Encyclopedia example (Create your own dialog box) _android

Source: Internet
Author: User
Tags stub

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.

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.

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.

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

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.

The following 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 when you press the return button to eject a prompt to ensure that the correct operation, using the common style of the dialog box.

Code: Creating 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.getrep Eatcount () = = 0) {
  dialog ();
 }
 return false;
 }

Figure 2 Effect: Change the dialog box chart, add 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 dialog, 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 (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, "not to be liked." ", 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: The information content is a group of radio boxes

New Alertdialog.builder (this). Settitle ("check box"). Setmultichoiceitems (
   new string[] {"Item1", "Item2"}, NULL, NULL) C6/>.setpositivebutton (OK, null)
   . Setnegativebutton ("Cancel", null). Show ();

Figure 5 Effect: 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 ();

Figure 6 Effect: 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 ();

Figure 7 Effect: The information content is a custom layout

1. layout file

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

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Related Article

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.