Android Apidemos Sample parsing (app->dialog)

Source: Internet
Author: User

The main activity definition of this example is used primarily in Alertdialogsamples.java to introduce the use of Class Alertdialog, Alertdialog provides a variety of functions:

Displays the message to the user and provides one to three buttons (OK, Cancel, Yes, No) to select or display a warning.

Displays a list for the user to choose from, the list can be radio button (radio), Check button (multiple selection)

Displays a text box to accept user input, and so on.

Creating Alertdialog is typically constructed by Alertdialog.builder:

Alertdialog.builder ad=new Alertdialog.builder

(context);

After that, you can set the caption for this alergdialog, display the information, need to display the buttons and so on. Then call Ad.show to display the dialog box.

To avoid creating a new dialog object each time the dialog box is displayed, Android provides two methods Oncreatedialog and Onpreparedialog events to manage the creation of the dialog box.

By overloading Oncreatedialog, you can create the required dialog box instances as needed, such as when performing ShowDialog. After you create this dialog instance, you can overload the Onpreparedialog method before each ShowDialog, if you need to make some changes to the dialog box. The principle is similar to the way Android manages menu.

The general steps for using Alertdialog are given below. Because it is possible to create multiple dialog examples in Oncreatedialog, you must first define the ID of a dialog.

private static final int dialog_yes_no_message = 1;

The Oncreatedialog is then overloaded, the parameter ID is dialog ID, and the desired dialog instance can be created based on the ID.

@Override 
protected Dialog oncreatedialog (int id) {     
 switch (ID) {case     
 dialog_yes_no_message:     
 return New Alertdialog.builder (Alertdialogsamples.this)     
 . SetIcon (R.drawable.alert_dialog_icon)     
 . SetTitle ( R.string.alert_dialog_two_buttons_title)     
 . Setpositivebutton (R.STRING.ALERT_DIALOG_OK,     
    new Dialoginterface.onclicklistener () {public     
 void OnClick (dialoginterface dialog, int whichbutton) {/     
          
 * User Clicked OK so do some stuff     
 /}     
 })     
 . Setnegativebutton (R.string.alert_dialog_cancel,     
     new Dialoginterface.onclicklistener () {public     
 void OnClick (dialoginterface dialog, int whichbutton) {/     
          
 * User Clicked Cancel so do some stuff 
 /}     
 )     
 . Create ();     
          
 ...

Show dialog

ShowDialog (Dialog_yes_no_message);

App->dialog uses eight examples to illustrate Alertdialog's multiple usages:

OK Cancel dialog with a message

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.