android dialog box

Source: Internet
Author: User

One: Alertdialog

Alertdialog's construction method is all protected, so it is not possible to create a alertdialog directly from the new Alertdialog.

To create a alertdialog, use the Create () method in the Alertdialog.builder.

Alertdialog.builder Common methods

Settitle: Setting a title for a dialog box
SetIcon: Setting icons for dialog boxes
Setmessage: Setting Content for dialog boxes
Setview: Setting Custom styles for dialog boxes
Setitems: A list of Settings dialog boxes to display, typically used to display several commands
Setmultichoiceitems: Used to set the dialog box to display a series of check boxes
Setneutralbutton: Normal button

Setpositivebutton: Add "Yes" button to the dialog box
Setnegativebutton: Dialog box Add "No" button
Create: Creating dialog box
Show: Show dialog box

import Android.os.bundle;import Android.app.activity;import android.app.AlertDialog.Builder; Import Android.app.AlertDialog; Import Android.view.menu;import android.view.view;import Android.widget.Button; Public classMainactivity extends Activity {Privatebutton button; @Overrideprotected voidonCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.activity_main); Button= (Button) This. Findviewbyid (R.id.button1); Button.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (View arg0) {//TODO auto-generated Method StubBuilder adinfo=NewAlertdialog.builder (mainactivity. This); Adinfo.settitle ("Simple dialog box");//Set TitleAdinfo.setmessage ("Test");//Set ContentAdinfo.seticon (R.drawable.ic_launcher);//Set iconadinfo.create ();            Adinfo.show ();    }        }); }

1.AlertDialog can display 3 buttons, now we add the display button

1. The Dialog.setpositivebutton button is displayed on the right, 2. Dialog.setnegativebutton display on the left 3.dialog.setneutralbutton button appears in the center

protected voidonCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.activity_main); Button= (Button) This. Findviewbyid (R.id.button1); Button.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (View arg0) {//TODO auto-generated Method StubBuilder adinfo=NewAlertdialog.builder (mainactivity. This); Adinfo.settitle ("Simple dialog box");//Set TitleAdinfo.setmessage ("Test");//Set ContentAdinfo.seticon (R.drawable.ic_launcher); Adinfo.setpositivebutton ("Cancel",NewDialoginterface.onclicklistener () {@Override Public voidOnClick (Dialoginterface Dialog,intwhich) {                            //write the code according to the actual situation.                      }                  }); //Register listener events for the Cancel buttonAdinfo.setnegativebutton ("Determine",NewDialoginterface.onclicklistener () {@Override Public voidOnClick (Dialoginterface Dialog,intwhich) {                            //write the code according to the actual situation.                      }                  });                  Adinfo.create ();            Adinfo.show ();    }        }); }

2.alertdialog+ buttons and check boxes

protected voidonCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.activity_main); Button= (Button) This. Findviewbyid (R.id.button1); Button.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (View arg0) {//TODO auto-generated Method StubString[] choices={"Facebook","Twitter"}; //Check array of judgments, corresponding to optionsBoolean[] Chsbool = {true,false}; //dialog box with multiple options and check boxesAlertdialog dialog =NewAlertdialog.builder (mainactivity. This). SetIcon (Android. r.drawable.btn_star_big_on). Settitle ("Survey"). Setmultichoiceitems (Choices, Chsbool, Multiclick). Setpositiveb Utton ("Yes", onclick). Setnegativebutton ("No", onclick). Create ();            Dialog.show ();    }        }); } Onmultichoiceclicklistener Multiclick=NewOnmultichoiceclicklistener () {@Override Public voidOnClick (Dialoginterface Dialog,intwhich, Boolean isChecked) {Toast.maketext (mainactivity). This,"Section"+ (which+1)+"item, select the result:"+ischecked,toast.length_short). Show ();             }                }; Onclicklistener onclick=NewOnclicklistener () {@Override Public voidOnClick (Dialoginterface Dialog,intwhich) {            //TODO auto-generated Method Stub             Switch(which) { CaseDialog.BUTTON_NEGATIVE:Toast.makeText (mainactivity. This,"No..", Toast.length_long). Show ();  Break;  CaseDialog.BUTTON_NEUTRAL:Toast.makeText (mainactivity. This,"I don ' t know.", Toast.length_long). Show ();  Break;  CaseDialog.BUTTON_POSITIVE:Toast.makeText (mainactivity. This,"yes!!", Toast.length_long). Show ();  Break; }       }    };
Second, custom Alertdialog

How do I customize a dialog box?

(1) Modify the System default dialog style (style, theme)

(2) Custom dialog Layout file

(3) You can encapsulate a class, inherit from dialog or directly use the dialog class to implement, in order to facilitate reuse later, it is recommended to encapsulate a dialog class.

Cond....

Iii. ProgressDialog1. Use of circular progress bars
 Public classMainactivity extends Activity {PrivateButton Button1,button2; //Declaring a Progress bar dialog box    PrivateProgressDialog pddialog=NULL; //Progress Count    intICount =0; @Overrideprotected voidonCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.activity_main); Button1= (Button) This. Findviewbyid (r.id.progress); Button2= (Button) This. Findviewbyid (r.id.circle); Button1.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (View arg0) {//TODO auto-generated Method StubICount =0; Pddialog=NewProgressDialog (mainactivity. This); //set the style of the progress bar, the style is round, the rotation of thePddialog.setprogressstyle (Progressdialog.style_spinner); //Set ProgressDialog titlePddialog.settitle ("Round progress bar"); //Set ProgressDialog prompt informationPddialog.setmessage ("Downloading in ..."); //set ProgressDialog title iconPddialog.seticon (R.drawable.ic_launcher); //Set ProgressDialog progress bar ProgressPddialog.setprogress ( -); //sets whether the ProgressDialog progress bar is ambiguousPddialog.setindeterminate (false); //sets whether ProgressDialog can be canceled by pressing the return buttonPddialog.setcancelable (true); //set a button for ProgressDialogPddialog.setbutton ("Cancel",NewDialoginterface.onclicklistener () { Public voidOnClick (Dialoginterface Dialog,inti) {//Click "Cancel" button to cancel the dialog boxDialog.cancel ();                        }                         });                                                Pddialog.show (); //creating a Thread instance                       NewThread () { Public voidrun () {Try{                                         while(ICount <= -) {                                            //the progress is controlled by the thread. Pddialog.setprogress (ICount + +)); Thread.Sleep ( -);                                    } pddialog.cancel (); }                                    Catch(interruptedexception e) {pddialog.cancel ();                        }}}.start ();        }            }); Button2.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (View arg0) {//TODO auto-generated Method Stub                                            }                }); }
2, the use of circular progress bar

Change its type to

Pddialog.setprogressstyle (progressdialog.style_horizontal);

android 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.