This experience will introduce the four kinds of Alertdialog in Android: Show with Neutral, Cancel, OK prompt box, get Dialog button with list, get dialog box with radio List, get dialog box with multiple options. This experience describes getting a dialog box with multiple options displayed
Android Studio or Eclipse
First we lay out four buttons on the top of the interface layout.
Then we associate these four buttons in the code
Button button3= (button) Findviewbyid (R.id.button3);
Button button4= (button) Findviewbyid (R.ID.BUTTON4);
Button button5= (button) Findviewbyid (R.ID.BUTTON5);
Button button6= (button) Findviewbyid (R.ID.BUTTON6);
Then add the triggering event separately:
//Get a dialog box button with a listButton4.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (View v) {FinalString[] Iterms=Newstring[]{"Run", "Badminton", "Football", "Pingpang", "gymnastics", "tennis", "more ..."}; Alertdialog.builder Builder=NewAlertdialog.builder (mainactivity. This); //Builder.seticon (r.drawable.desert);Builder.settitle ("Choose Your favable Sport project:"); Builder.setitems (Iterms,NewDialoginterface.onclicklistener () {@Override Public voidOnClick (Dialoginterface Dialog,intwhich) {Toast.maketext (Getapplicationcontext (),"You choosed" +Iterms[which],toast.length_long). Show (); } }); Builder.create (). Show ();//creates a dialog box and displays } }); //Get a dialog box button with a single-selection listButton5.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (View v) {FinalString[] Iterms=Newstring[]{"Standard", "silent", "meeting", "Outdoor", "Offline"}; Alertdialog.builder Builder=NewAlertdialog.builder (mainactivity. This); //Builder.seticon (r.drawable.desert);Builder.settitle ("Choose your favable contextual model:"); Builder.setsinglechoiceitems (Iterms,0,NewDialoginterface.onclicklistener () {@Override Public voidOnClick (Dialoginterface Dialog,intwhich) {Toast.maketext (Getapplicationcontext (),"You choosed" +Iterms[which], Toast.length_long). Show (); } }); Builder.create (). Show ();//creates a dialog box and displays } }); //get a dialog box with multiple options displayedButton6.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (View v) {CheckedItems=New Boolean[]{false,true,false,true,false}; Items=Newstring[]{"Plants vs Zombies", "Angry Birds", "Popodino", "Happy Farm", "Super Mario"}; //Display a dialog box with a single-selection listAlertdialog.builder builder=NewAlertdialog.builder (Getapplicationcontext ()); //Builder.seticon (r.drawable.desert);Builder.settitle ("Choose Your favorable Game:"); Builder.setmultichoiceitems (Items,checkeditems,NewDialoginterface.onmultichoiceclicklistener () {@Override Public voidOnClick (Dialoginterface Dialog,intWhich,BooleanisChecked) {Checkeditems[which]=isChecked; } }); //Add the OK button to the dialog boxBuilder.setpositivebutton ("Confirm",NewDialoginterface.onclicklistener () {@Override Public voidOnClick (Dialoginterface Dialog,intwhich) {String result=""; for(inti=0;i<checkeditems.length;i++){ if(checkeditems[i]==true) {result+=items[i]+ ","; } } if(Result.equals ("") = =false) {result=result.substring (0,result.length ()-1); Toast.maketext (mainactivity. This, "You choosed" +Result,toast.length_long). Show (); } } }); Builder.create (). Show (); } });
The operation results are as follows
More detailed introduction You can come here to see, illustrated more clearly:
Http://jingyan.baidu.com/article/0320e2c1ccc8031b87507b23.html
android--Four kinds of aterdialog