Android Dialog dialog box and sample code _android

Source: Internet
Author: User
Tags event listener

Android Dialog dialog box

1, Dialog Introduction

2, the basic use of Alertdialog

3, custom dialog box custom Dialog

First, Dialog introduction

Dialog is also a common user interface element in Android, and he is not a subclass of view like menu. Let's take a look at its inheritance relationship:

Here, take a look at his direct subclass Alertdialog, and the indirect subclass Datepickerdialog,progressdialog,timepickerdialog, the last three of which we've talked about in the previous chapters, Today we focus on the alertdialog.

Second, the basic use of Alertdialog

The Alertdialog dialog box is a dialog subclass that provides an icon, a caption, a text, and 3 buttons. We can create and display dialog on our own activity, or we can manage it through activity methods. We can learn how to use it in the following example, and also note the comments in the code.

1, create a project lesson17_helloalertdialog,activity's file named Mainhelloalertdialog.java

2, the contents of the Res/layout/main.xml are as follows:

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout android:layout_height= "Fill_parent" Android:layout_ Width= "fill_parent" android:orientation= "vertical" xmlns:android= "http://schemas.android.com/apk/res/android" > <textview android:layout_height= "wrap_content" android:layout_width= wrap_content "android:id=" @+id/ TextView01 "android:text=" dialog box example "android:textsize=" 20sp "android:layout_margintop=" 5DP "> </textview> < Button android:layout_height= "wrap_content" android:layout_width= "wrap_content" android:id= "@+id/Button01" Android : text= Display dialog Box | ShowDialog () "android:textsize=" 20sp "android:layout_margintop=" 5DP "> </button> <button android:layout_ height= "Wrap_content" android:layout_width= "wrap_content" android:id= "@+id/button02" android:text= "Close dialog box | DismissDialog () "android:textsize=" 20sp "android:layout_margintop=" 5DP "> </button> <button android: layout_height= "Wrap_content" android:layout_width= "wrap_content" android:id= "@+"Id/button03 "android:text=" Removal Dialog |removedialog () "android:textsize=" 20sp "android:layout_margintop=" 5DP "> </ Button> </linearlayout>

The contents of the

3, Mainhelloalertdialog.java are as follows:

Package android.basic.lesson17;
Import android.app.Activity;
Import Android.app.AlertDialog;
Import Android.app.Dialog;
Import Android.content.DialogInterface;
Import Android.content.DialogInterface.OnClickListener;
Import Android.os.Bundle;
Import Android.view.View;
Import Android.widget.Button;

Import Android.widget.Toast;

  public class Mainhelloalertdialog extends activity {//define the id int edward_movie_dialog = 1 of a dialog box; Dialog button click event Listener Onclicklistener OCL = new Onclicklistener () {@Override public void OnClick (Dialoginterface dial OG, int which) {switch (which) {Case Dialog.BUTTON_NEGATIVE:Toast.makeText (Mainhelloalertdialog.thi S, "I don't like his movies."
        ", Toast.length_long). Show ();
      Break Case Dialog.BUTTON_NEUTRAL:Toast.makeText (Mainhelloalertdialog.this, "I don't like it."
        ", Toast.length_long). Show ();
      Break Case Dialog.BUTTON_POSITIVE:Toast.makeText (Mainhelloalertdialog.this, "I like his movies very much.")
    ",        Toast.length_long). Show ();
      Break

  }
    }
  }; @Override/** called when the "activity is" is a created.
    * * public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);

    Setcontentview (R.layout.main); Define dialog Object Dialog Dialog = new Alertdialog.builder (this). SetIcon (Android. R.drawable.btn_star). Settitle ("Hobby Survey"). Setmessage ("Do you like watching Edward Norton's movies?")

    "). Setnegativebutton (" Not like ", OCL). Setneutralbutton (" General ", OCL). Setpositivebutton (" Very like ", OCL). Create ();

    Display dialog box Dialog.show ();
    Defines the button UI component button B1 = (button) Findviewbyid (R.ID.BUTTON01);
    Button B2 = (button) Findviewbyid (R.ID.BUTTON02);

    Button B3 = (button) Findviewbyid (R.ID.BUTTON03); Defines a button's Click event Listener View.onclicklistener b_ocl= New View.onclicklistener () {@Override public void OnClick (Vie W v) {switch (V.getid ()) {case R.ID.BUTTON01://Display dialog box ShowDialog (edward_movie_dialOG);
        Break
          Case R.ID.BUTTON02://Close the dialog box This function is good silly x, the button dismissdialog (edward_movie_dialog);
        Break
          Case R.ID.BUTTON03://Remove dialog box This function is so silly x, the button Removedialog (Edward_movie_dialog) that cannot be clicked at all;
        Break

    }
      }
    };
    Bind button Listener B1.setonclicklistener (B_OCL);
    B2.setonclicklistener (B_OCL);

  B3.setonclicklistener (B_OCL); @Override public Dialog oncreatedialog (int id) {Toast.maketext is invoked when the session box is created (this, Oncreatedialog method is called, Toas

    T.length_long). Show (); Return to New Alertdialog.builder (this). SetIcon (Android. R.drawable.btn_star). Settitle ("Hobby Survey"). Setmessage ("Do you like watching Edward Norton's movies?")
  "). Setnegativebutton (" Not like ", OCL). Setneutralbutton (" General ", OCL). Setpositivebutton (" Very like ", OCL). Create (); It will be invoked @Override public void onpreparedialog (int id, Dialog Dialog) {Toast.maketext Each time the dialog box is displayed (this, onprepar
     Edialog method is called, Toast.length_long. Show (); Super.onPreparedialog (ID, Dialog); }

}

4, the results of the operation are as follows:

Interested students can consider how to improve the close and Remove dialog box buttons.

The above is to the Android Dialog data collation, follow-up continue to add, thank you for your support of this site!

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.