Android Basics (13)--Using the dialog box

Source: Internet
Author: User
Tags event listener

Turn from: Use of the android dialog box

A dialog box is usually a small window that is displayed before the current activity. The following activity loses focus and the dialog box above receives the user's interaction information. dialog boxes are often used as hints and as a short-stay interface directly related to the program's running process.

Alertdialog

Description: One can handle 0, one, 2, or 3 buttons, and/or a set of items that can be selected, including check boxes or radio buttons. Alertdialog is adequate for most dialog boxes for creating and user interaction, and it is also the recommended type of dialog box. Specific as follows:

Create a Alertdialog

A Alertdialog dialog box inherits from the dialog class. You should use the following features to decorate your dialog box:

L a Title

L A piece of information

L 1, 2, or 3 buttons

L A set of items to choose from (check box or radio button)

In order to create a alertdialog, you can use the Alertdialog.builder subclass. Use

Alertdialog.builder (Context) obtains a dialog constructor and then uses the common method of the class to define all the properties of the Alertdialog dialog box. After you have set the properties using the dialog constructor, call the Create () method to get a Alertdialog object.

Add button

To create a Alertdialog dialog box with buttons next to each other, as shown, you can use set ... Button () Method:

The main code is as follows:

BTN1 = (Button) Findviewbyid (r.id. Dlg_with_btns);

Click the button to create the Alertdialog dialog box

Btn1.setonclicklistener (new Onclicklistener () {

Public void OnClick (view view) {

Create a Dialog builder first

Alertdialog.builder Builder =

New Alertdialog.builder (mainactivity. this);

After you create the properties of the Settings dialog box

Title

Builder.setmessage ("Welcome to my blog on CSDN?")

Cannot be canceled (that is, the return key cannot cancel this dialog box)

. setcancelable (false)

Set the label of the first button and its event listener

. Setpositivebutton ("good",

New Dialoginterface.onclicklistener () {

Public void OnClick (Dialoginterface Dialog,

int id) {

// ..

}

})

Set the label of the second button and its event listener

. Setnegativebutton ("Need improving",

New Dialoginterface.onclicklistener () {

Public void OnClick (Dialoginterface Dialog,

int id) {

// ..

}

});

Create a dialog box with the dialog builder

Alertdialog alert = Builder.create ();

Show dialog box

Alert.show ();

}

});

First, call Setmessage (charsequence) to set the caption of the dialog box. Then, the method chain is started and the Setcancelable (Boolean) Setting dialog box cannot be canceled (the user cannot use the return key to close the dialog box). For each button, use set ... One of the button () methods, such as Setpositivebutton (), requires the label of the incoming button and the Dialoginterface.onclicklistener event listener for the preset action when the user taps the button. (Use Setneutralbutton () to set the middle button)

Note: For each type of button you can just add one for alertdialog. In other words, you cannot have more than one "positive" button. This limits the maximum number of buttons that may be in the picture: positive, neutral, negative. These names are technically irrelevant to the actual functionality of your buttons, but should help you track what each button does.

Add a list

Like, to add a series of selectable lists to Alertdialog, you can use the Setitems () method.

The specific code is as follows:

BTN2 = (Button) Findviewbyid (r.id. Dlg_with_list);

Btn2.setonclicklistener (new Onclicklistener () {

Public void OnClick (view view) {

Final charsequence[] Items =

{"Engineer", "Student", "it-_-!"};

Alertdialog.builder Builder =

New Alertdialog.builder (mainactivity. this);

Builder.settitle ("What ' s your job?")

. Setitems (items, new Dialoginterface.onclicklistener () {

Public void OnClick (dialoginterface dialog, int item) {

Toast. Maketext (Getapplicationcontext (), Items[item],

Toast. length_short). Show ();

}

});

Alertdialog alert = Builder.create ();

Alert.show ();

}

});

First, set the caption of the dialog box with the Settitle (charsequence) method. Then, call Setitems () to add a series of selectable items that receive an optional array of display content and a Dialoginterface.onclicklistener event listener that defines the action that occurs when the user selects an item.

Add check boxes and radio buttons

You can use the Setmultichoiceitems () and Setsinglechoiceitems () methods separately in order to create a series of multiple items (checkboxes) or single-selection items (radio boxes) inside a dialog box. If you create an optional item in the Oncreatedialog () fallback method, the Android system will manage the status of this set of optional items for you. As long as the activity is active, the dialog will remember the state before the optional items, but when the user leaves the activity, the previous selection state is lost.

The specific code is as follows:

Btn3.setonclicklistener (new Onclicklistener () {

Public void OnClick (view view) {

Final charsequence[] Items =

{"Nanjing", "Shanghai", "Yanchen", "Other.."};

Alertdialog.builder Builder =

New Alertdialog.builder (mainactivity. this);

Builder.settitle ("Where Do you live?");

Builder.setsinglechoiceitems (items,-1,

New Dialoginterface.onclicklistener () {

Public void OnClick (dialoginterface dialog, int item) {

Toast. Maketext (Getapplicationcontext (), Items[item],

Toast. length_short). Show ();

}

});

Alertdialog alert = Builder.create ();

Alert.show ();

}

});

Tip: To save the selection state when the user leaves or pauses the activity, you must save or restore the settings in the activity's life cycle. In order to permanently save the selection state, you need to use a data storage technology to save the settings.

Create a custom dialog box

If you want to have a custom dialog box, you can create your own layout for the dialog box. After you have defined the layout, you can pass the object or layout resource ID of the root Class View to Setcontentview (view).

The Customize dialog box looks like this:

For example, in order to create a dialog box like this, the following steps are required:

1. Create a dialog layout file, such as create a custom_dialog.xml file under Layout, with the following contents:

<?xml version= "1.0" encoding= "Utf-8"?>

<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"

Android:id= "@+id/layout_root"

android:orientation= "Horizontal"

Android:layout_width= "Fill_parent"

android:layout_height= "Fill_parent"

android:padding= "10dip" >

<imageview android:id= "@+id/image"

Android:layout_width= "Wrap_content"

android:layout_height= "Fill_parent"

android:layout_marginright= "10dip"/>

<textview android:id= "@+id/text"

Android:layout_width= "Wrap_content"

android:layout_height= "Fill_parent"

Android:textcolor= "#FFFFFF"/>

</LinearLayout>

This XML layout file defines a imageview and TextView in LinearLayout (linear layout).

2. Set the layout file above as the layout of the dialog box, and define the content elements for ImageView and TextView:

Btn4 = (Button) Findviewbyid (r.id. Dlg_customized);

Btn4.setonclicklistener (new Onclicklistener () {

Public void OnClick (view view) {

Dialog Dialog = new Dialog (mainactivity. this);

Dialog.setcontentview (r.layout. Custom_dialog);

Dialog.settitle ("Custom Dialog");

TextView Text = (TextView)

Dialog.findviewbyid (r.id. Text);

Text.settext ("Hello, this is a custom dialog!");

ImageView image = (ImageView)

Dialog.findviewbyid (r.id. Image);

Image.setimageresource (r.drawable. Icon);

Dialog.show ();

}

});

After you initialize the dialog box, use Setcontentview (int) To set the layout of the dialog box to a custom layout that requires the passed in parameter as the ID of the layout file. Now that the dialog box has a layout defined, you can use Findviewbyid (int) to find the components, and you can change their display.

places to be aware of:

1) at the bottom of the dialog layout file is used in order to find the image and text components

Dialog.findviewbyid, otherwise the program will appear abnormally and exit!

2) At last don't forget to Dialog.show ();

Summary: This article briefly introduces the use of the dialog box, which is generally more commonly used is the first dialog box, concise and clear, the last dialog box with a complex layout, if you can use good words, can achieve very good results.

Android Basics (13)--Using the dialog box

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.