Alertdialog (dialog box)
First, the Basic use of dialog box flow
Step1: Create Alertdialog.buider;
Step2: Call SetIcon () settings icon, Settitle () or Setcustomertitle set caption
Step3: Setting the contents of the dialog box Setmessage () there are other ways;
Step4:setposition/negative/naturalbutton settings: OK, Cancel, neutral
STEP5: Calling the Create () method creates this object and displays the dialog box in the call Show () method
Second, several commonly used dialog boxes have Custom View dialog box use instance
PackageCom.example.test3;Importandroid.app.Activity;ImportAndroid.app.AlertDialog;ImportAndroid.content.DialogInterface;ImportAndroid.os.Bundle;ImportAndroid.view.View;ImportAndroid.widget.Button;ImportAndroid.widget.Toast; Public classMainactivityextendsActivityImplementsview.onclicklistener{PrivateButton btn1; PrivateButton btn2; PrivateButton Btn3; PrivateButton Btn4; PrivateButton btn5; PrivateAlertdialog.builder Builder; PrivateAlertdialog Alertdialog; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); BTN1=(Button) Findviewbyid (R.ID.BTN1); BTN2=(Button) Findviewbyid (R.ID.BTN2); Btn3=(Button) Findviewbyid (R.ID.BTN3); Btn4=(Button) Findviewbyid (R.ID.BTN4); Btn5=(Button) Findviewbyid (R.ID.BTN5); Btn1.setonclicklistener ( This); Btn2.setonclicklistener ( This); Btn3.setonclicklistener ( This); Btn4.setonclicklistener ( This); Btn5.setonclicklistener ( This); } @Override Public voidOnClick (view view) {Switch(View.getid ()) { Caser.id.btn1:{Builder=NewAlertdialog.builder (mainactivity. This); Alertdialog=Builder.seticon (r.mipmap.ic_launcher). Settitle ("System prompt:"). Setmessage ("This is one of the most common alertdialog,\n with three buttons, namely Cancel, OK, neutral"). Setnegativebutton ("Cancel",NewDialoginterface.onclicklistener () {@Override Public voidOnClick (Dialoginterface Dialoginterface,inti) {}}). Setpositivebutton ("OK",NewDialoginterface.onclicklistener () {@Override Public voidOnClick (Dialoginterface Dialoginterface,inti) {}}). Setneutralbutton ("Neutral",NewDialoginterface.onclicklistener () {@Override Public voidOnClick (Dialoginterface Dialoginterface,inti) {}}). Create (); Alertdialog.show (); Break; } Caser.id.btn2:{Finalstring[] Lesson =Newstring[]{"Chinese", "mathematics", "English", "Chemistry", "Biology", "Physics", "Sports"}; Builder=NewAlertdialog.builder (mainactivity. This); Alertdialog=Builder.seticon (r.mipmap.ic_launcher). Settitle ("Choose the course you like"). Setitems (Lesson,NewDialoginterface.onclicklistener () {@Override Public voidOnClick (Dialoginterface Dialog,intwhich) {Toast.maketext (Getapplicationcontext (),"You have chosen" +Lesson[which], toast.length_short). Show (); }}). Create (); Alertdialog.show (); Break; } Caser.id.btn3:{Finalstring[] Fruits =Newstring[]{"Apple", "Sydney", "banana", "grape", "watermelon"}; Builder=NewAlertdialog.builder (mainactivity. This); Alertdialog=Builder.seticon (r.mipmap.ic_launcher). Settitle ("Choose the fruit you like, only one Oh ~"). Setsinglechoiceitems (Fruits,0,NewDialoginterface.onclicklistener () {@Override Public voidOnClick (Dialoginterface Dialog,intwhich) {Toast.maketext (Getapplicationcontext (),"You have chosen" +Fruits[which], toast.length_short). Show (); }}). Create (); Alertdialog.show (); Break; } Caser.id.btn4:{FinalString[] Menu =Newstring[]{"boiled tofu", "radish brisket", "soy sauce chicken", "Pepper pork belly Chicken"}; //defines a Boolean array that is used to record the state of a list item Final Boolean[] Checkitems =New Boolean[]{false,false,false,false}; Builder=NewAlertdialog.builder (mainactivity. This); Alertdialog=Builder.seticon (r.mipmap.ic_launcher). Setmultichoiceitems (menu, Checkitems,NewDialoginterface.onmultichoiceclicklistener () {@Override Public voidOnClick (Dialoginterface Dialog,intWhich,BooleanisChecked) {Checkitems[which]=isChecked; }}). Setpositivebutton ("OK",NewDialoginterface.onclicklistener () {@Override Public voidOnClick (Dialoginterface Dialog,intwhich) {String result= ""; for(inti = 0; i < checkitems.length; i++) { if(checkitems[i]) result+ = Menu[i] + ""; } toast.maketext (Getapplicationcontext (),"Sir you point out:" +result, Toast.length_short). Show (); }}). Create (); Alertdialog.show (); Break; } Caser.id.btn5:{Builder=NewAlertdialog.builder (mainactivity. This); View View1= Getlayoutinflater (). Inflate (R.layout.view_dialog,NULL); Builder.setview (View1); Alertdialog=builder.create (); Alertdialog.show (); Button BTN1=(Button) View1.findviewbyid (r.id.btn_cancle); Button btn2=(Button) View1.findviewbyid (R.id.btn_blog); Button Btn3=(Button) View1.findviewbyid (r.id.btn_close); Btn1.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (view view) {Alertdialog.dismiss (); } }); Btn2.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (view view) {Alertdialog.dismiss (); } }); Btn3.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (view view) {Alertdialog.dismiss (); } }); Break; } } }}
Android Tips & Dialogs----Alertdialog