android--Four kinds of aterdialog

Source: Internet
Author: User

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

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.