View--dialog box

Source: Internet
Author: User

dialog box

I. Classification

Warning dialog box Alertdialog:① General Dialog ② Radio dialog box ③ Check dialog Box ④ custom dialog box

Progress dialog box

Date dialog box

Time dialog box

Two. Warning dialog box Alertdialong

1. General

cannot be instantiated directly using

Using the internal constructor to generate a dialog box

New Alertdialog.builder (context) instantiation constructor: (1) Settitle (title) (2) setmessage (message)

(3) button: ① Confirm button Setpositivebutton ("Content", click event Listener) ② deny button Setnegativebutton ("Text", click event Listener) ③ neutral button Setneutralbutton ("Text", Click event Listener)

(4) Show dialog box after creation and return to Alertdialog instance (5) creat () Build dialog box and return

(6) Setcancelable (true/false): ① Set If you can click anywhere else to close the dialog ② similar to the modal window

2. Single-selection dialog box

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"Android:paddingbottom= "@dimen/activity_vertical_margin"Android:paddingleft= "@dimen/activity_horizontal_margin"Android:paddingright= "@dimen/activity_horizontal_margin"Android:paddingtop= "@dimen/activity_vertical_margin"Tools:context= "Com.hanqi.testapp2.TestActivity5"android:orientation= "Vertical" > <Button android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:text= "General dialog box"Android:onclick= "Bt1_onclick"/> <Button android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:text= "Radio dialog box"Android:onclick= "Bt2_onclick"/> <Button android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:text= "Check dialog box"Android:onclick= "Bt3_onclick"/></linearlayout>

 Packagecom.hanqi.testapp2;ImportAndroid.app.AlertDialog;ImportAndroid.content.DialogInterface;Importandroid.support.v7.app.AppCompatActivity;ImportAndroid.os.Bundle;ImportAndroid.view.View;ImportAndroid.widget.Toast; Public classTestActivity5extendsappcompatactivity {@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);    Setcontentview (R.LAYOUT.ACTIVITY_TEST5); }    //General dialog Box     Public voidBt1_onclick (View v) {//dialog box cannot be instantiated directly//The constructor is provided internally//Method Chain CallAlertdialog alertdialog=NewAlertdialog.builder ( This). Settitle ("Confirmation dialog box"). Setmessage ("Are you sure you want to delete it?" "). Setpositivebutton ("Confirm",NewDialoginterface.onclicklistener () {@Override Public voidOnClick (Dialoginterface Dialog,intwhich) {Toast.maketext (TestActivity5). This, "perform delete, which=" +which, Toast.length_short). Show (); }                })//Front button. Setnegativebutton ("Cancel",NewDialoginterface.onclicklistener () {@Override Public voidOnClick (Dialoginterface Dialog,intwhich) {Toast.maketext (TestActivity5). This, "undelete, which=" +which, Toast.length_short). Show (); }                })//Negative button. Setneutralbutton ("neutral",NewDialoginterface.onclicklistener () {@Override Public voidOnClick (Dialoginterface Dialog,intwhich) {Toast.maketext (TestActivity5). This, "normal button, which" +which, Toast.length_short). Show (); }}). setcancelable (false). Show ();//Show dialog Box    }    //Single-selection dialog box     Public voidBt2_onclick (View v) {//final can extend the life cycle of this constant to the entire object        FinalString [] str={"male", "female"}; NewAlertdialog.builder ( This). Settitle ("Radio dialog box"). Setsinglechoiceitems (str,0,NewDialoginterface.onclicklistener () {@Override Public voidOnClick (Dialoginterface Dialog,intwhich) {Toast.maketext (TestActivity5). This, "which=" +which+ ", selected" +Str[which], toast.length_short). Show (); //Close the dialog boxDialog.dismiss (); }}). setcancelable (false). Show (); }    //Check dialog box     Public voidBt3_onclick (View v) {FinalString [] str={"BMW", "Mercedes", "Rolls-Royce", "Bentley"}; Final Boolean[] ch={true,false,false,true}; NewAlertdialog.builder ( This). Settitle ("Check dialog box"). Setmultichoiceitems (str, CH,NewDialoginterface.onmultichoiceclicklistener () {@Override Public voidOnClick (Dialoginterface Dialog,intWhich,BooleanisChecked) {                        //Change the selected state of the corresponding array itemch[which]=isChecked; }}). Setpositivebutton ("Confirm",NewDialoginterface.onclicklistener () {@Override Public voidOnClick (Dialoginterface Dialog,intwhich) {                        intI=0; //get the final check state                         for(Booleanb:ch) {Toast.maketext (TestActivity5). This, str[i]+ "Checked status =" + (b? ") Check ":" Unchecked "), Toast.length_long). Show (); I++; }}). Setnegativebutton ("Cancel",NULL). Setcancelable (false). Show (); }}

View--dialog box

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.