Android Alertdialog A variety of dialog box usage examples _android

Source: Internet
Author: User

Target effect:

program run, show a few buttons of figure one, click on the button to show figure two to figure Six dialog box, click on a dialog box or button, will also show the corresponding toast output.

The 1.activity_main.xml page holds five buttons.

Activity_main.xml page:

<relativelayout 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 "Tools:context" =". Mainactivity "> <button android:id=" @+id/btnsure "android:layout_width=" Match_parent "android:layout_height=" 
Wrap_content "android:layout_margintop=" 10DP "android:text=" confirmation Dialog "/> <button android:id=" @+id/btnRadio " Android:layout_width= "Match_parent" android:layout_height= "wrap_content" android:layout_margintop= "60DP" Android: text= "/> <button android:id= @+id/btncheck" android:layout_width= "Radio dialog box" Match_parent 
"Wrap_content" android:layout_margintop= "110DP" android:text= "Multiple selection Dialog"/> <button android:id= "@+id/btnList" Android:layout_width= "Match_parent" android:layout_height= "wrap_content" android:layout_margintop= "160DP" Android : text= List Dialog "/> <button android:id=" @+id/btnmy "Android:layout_width=" MaTch_parent "android:layout_height=" wrap_content android:layout_margintop= "210DP" android:text= "Custom Dialog"/> </  Relativelayout>

2. Create a new Dialog.xml page as the layout page for the last custom dialog box.

Dialog.xml page:

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android= "http://schemas.android.com/apk/res/" Android "Android:layout_width=" Wrap_content "android:layout_height=" wrap_content "android:orientation=" vertical " > <linearlayout android:layout_width= "match_parent" android:layout_height= "Wrap_content" android:orientation = "Horizontal" > <edittext android:id= "@+id/edinput" android:layout_width= "Wrap_content" android:layout_height = "Wrap_content" android:layout_weight= "2" > <requestfocus/> </EditText> <button android:id= "@+id/b Tnok "android:layout_width=" wrap_content "android:layout_height=" Wrap_content "android:layout_weight=" 1 "android: 
text= "OK"/> </LinearLayout> <imageview android:id= "@+id/ivpicture" android:layout_width= "Wrap_content" android:layout_height= "280DP" android:src= "@drawable/white"/> <textview android:id= "@+id/textview1" Android: layout_gravity= "center" android:layout_width= "Wrap_coNtent "android:layout_height=" wrap_content "android:text=" TextView "/> </LinearLayout>  

3.mainactivity.java page Processing dialog box pop-up and click events.

Mainactivity.java page:

Package com.example.alertdialog; 
Import Android.os.Bundle; 
Import android.app.Activity; 
Import Android.app.AlertDialog; 
Import Android.content.DialogInterface; 
Import Android.view.LayoutInflater; 
Import Android.view.Menu; 
Import Android.view.View; 
Import Android.view.View.OnClickListener; 
Import Android.widget.Button; 
Import Android.widget.Toast; public class Mainactivity extends activity implements Onclicklistener {private Button btnsure,btnradio,btncheck,btnlist 
, btnmy; Private string[] sexlist={"male", "female"};//list private string[] likelist={"basketball", "football", "Play games", "Listen to Music", "See movie"};//Multiple selection list private String[] itemlist={"project manager", "plan", "Test", "Art", "Programmer"};//list @Override protected void OnCreate (Bundle savedinstancestate) {su 
Per.oncreate (savedinstancestate); 
Setcontentview (R.layout.activity_main); GetId ()///Get control ID Click ()//button binding fixed-point hit event}/* Get control id*/private void GetId () {btnsure = (Button) Findviewbyid (r.id.btnsure) 
; 
btnradio= (Button) Findviewbyid (R.id.btnradio); btncheck= (Button) FindviEwbyid (R.id.btncheck); 
Btnlist= (Button) Findviewbyid (r.id.btnlist); 
Btnmy= (Button) Findviewbyid (r.id.btnmy); 
/* button-binding fixed-point hit Event/private void Click () {Btnsure.setonclicklistener (this); 
Btnradio.setonclicklistener (this); 
Btncheck.setonclicklistener (this); 
Btnlist.setonclicklistener (this); 
Btnmy.setonclicklistener (this); 
@Override public boolean Oncreateoptionsmenu (Menu menu) {getmenuinflater (). Inflate (R.menu.main, menu); 
return true;  
@Override public void OnClick (view view) {switch (View.getid ()) {case r.id.btnsure:showdialog1 ();//confirmation dialog box break; 
Case R.ID.BTNRADIO:SHOWDIALOG2 ();//Radio dialog box break; 
Case R.id.btncheck:showdialog3 ();//Multiple-selection dialog box break; 
Case R.id.btnlist:showdialog4 (); 
Break 
Case R.id.btnmy:showdialog5 (); 
Break 
}/* The confirmation dialog box */private void ShowDialog1 () {Alertdialog.builder builder=new alertdialog.builder (this); Builder.settitle ("Confirmation Dialog");//Set the title Builder.seticon (r.drawable.ic_launcher);//Set icon Builder.setmessage ("confirmation dialog box prompt content") ;//Set content/* Add todialog box to determine the button and click event/Builder.setpositivebutton ("OK", new Dialoginterface.onclicklistener () {@Override public void OnClick ( 
Dialoginterface arg0, int arg1) {toast.maketext (Mainactivity.this, "Click OK button", Toast.length_short). Show (); 
} 
}); /* Add dialog box Cancel button and click event * * Builder.setnegativebutton ("Cancel", new Dialoginterface.onclicklistener () {@Override public void 
OnClick (dialoginterface arg0, int arg1) {toast.maketext (Mainactivity.this, "Click the Cancel button", Toast.length_short). Show (); 
} 
}); Alertdialog dialog=builder.create ()//Get Dialog Dialog.show ()//Display dialog box}/* Radio dialog box * * private void ShowDialog2 () {ALERTDI Alog. 
Builder builder=new Alertdialog.builder (this); Builder.settitle ("gender");/set caption Builder.seticon (r.drawable.ic_launcher);//Set icon//Parameter a single select list text, parameter two is selected by default (-1 is not selected by default), Parameter three is create Listener/builder.setsinglechoiceitems (sexlist,-1,new Dialoginterface.onclicklistener () {@Override public void 
OnClick (dialoginterface dialog, int which) {String Sex=sexlist[which]; Toast.maketext (Mainactivity.this, "This man has sex for" +sex, TOast. 
Length_short). Show (); 
} 
}); /* Add dialog box Cancel button click event/Builder.setnegativebutton ("Cancel", new Dialoginterface.onclicklistener () {@Override public void 
OnClick (dialoginterface dialog, int which) {Dialog.dismiss ();//Close Dialog}}); Alertdialog dialog=builder.create ()//Get Dialog Dialog.show ()//Display dialog box}//Multiple-selection dialog box * * private void ShowDialog3 () {ALERTDI Alog. 
Builder builder=new Alertdialog.builder (this); Builder.settitle ("hobby");/Set title Builder.seticon (r.drawable.ic_launcher);//Set icon//parameter same as Radio dialog box, the second parameter defaults to NULL, 
Instead of -1*/Builder.setmultichoiceitems (likelist,null,new dialoginterface.onmultichoiceclicklistener () {@Override public void OnClick (Dialoginterface dialog, int which, Boolean ischecked) {if (ischecked) {Toast.maketext (mainactivity.t 
His, "I Like" +likelist[which],toast.length_short. Show (); 
}else{Toast.maketext (Mainactivity.this, "I don't like" +likelist[which],toast.length_short). Show (); 
} 
} 
}); /* Add dialog box Cancel button click event/Builder.setnegativebutton ("Cancel", new Dialoginterface.onclicklistener (){@Override public void OnClick (Dialoginterface dialog, int which) {Dialog.dismiss ();//Close Dialog}}); Alertdialog dialog=builder.create ()//Get Dialog Dialog.show ()//Display dialog box}/* List dialog * * private void ShowDialog4 () {ALERTDI Alog. 
Builder builder=new Alertdialog.builder (this); Builder.settitle ("department list");/Set the title Builder.seticon (r.drawable.ic_launcher);//Set icon Builder.setitems (itemlist,new Dialoginterface.onclicklistener () {@Override public void OnClick (Dialoginterface dialog, int which) {Toast.maketext (M 
Ainactivity.this, "I clicked on" +itemlist[which],toast.length_short). Show (); 
} 
}); Alertdialog dialog=builder.create ()//Get Dialog Dialog.show ()//Display dialog box}///Custom dialog box * * private void ShowDialog5 () {Layout 
Inflater Inflater=layoutinflater.from (this); View view=inflater.inflate (r.layout.dialog,null)//Get a custom layout Alertdialog.builder builder=new Alertdialog.builder ( 
this); Builder.settitle ("Custom dialog box");/Set the title Builder.seticon (r.drawable.ic_launcher);//Set icon Builder.setview (view); To set a custom style layout to a dialog box
Alertdialog dialog=builder.create ()//Get Dialog Dialog.show ();//Show dialog box}} 

4. The operation will have the goal effect.

About Android Alertdialog the use of various dialog boxes to introduce you so much, I hope to help you!

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.