Layout file xml:
1 <linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" 2 xmlns:tools= "/HTTP/ Schemas.android.com/tools "3 android:layout_width=" match_parent "4 android:layout_height=" match_parent "5 Android oid:orientation= "vertical" 6 android:paddingbottom= "@dimen/activity_vertical_margin" 7 android:paddingleft= "@dimen /activity_horizontal_margin "8 android:paddingright=" @dimen/activity_horizontal_margin "9 android:paddingTop=" @dim En/activity_vertical_margin "Ten tools:context=". Dialogactivity ">11 <button13 android:id=" @+id/plaindialog [android:layout_width=] Match_par Ent "android:layout_height=" wrap_content "android:text=" General Dialog "/>17 <button19 Android:id= "@+id/plaindialogevent" android:layout_width= "Match_parent" android:layout_height= Tent "android:text=" dialog button event Centralized processing "/>23 <button25 android:id=" @+id/inputdialog "26 android:layout_width= "Match_parent" android:layout_height= "wrap_content" android:text= "Please enter box" />29 <button31 android:id= "@+id/listdialog" android:layout_width= "Match_parent" 33 android:layout_height= "wrap_content" android:text= "List dialog box"/>35 <button37 android:id= "@+id/r Adiodialog "android:layout_width=" match_parent "android:layout_height=" Wrap_content "Android : text= "Single-selection Dialog"/>41 <button43 android:id= "@+id/checkboxdialog" [android:layout_width=] Match_p Arent "android:layout_height=" wrap_content "android:text=" Multi-Select dialog box/>47 <button49 a Ndroid:id= "@+id/diydialog" android:layout_width= "match_parent" android:layout_height= "Wrap_content" 52 android:text= "Custom Layout Dialog"/>53 </LinearLayout>
Activity file:
Common dialog:
1 private void Plaindialogdemo () {2 3 button plainbtn = (button) Findviewbyid (R.id.plaindialog); 4 Plai Nbtn.setonclicklistener (New Onclicklistener () {5 6 public void OnClick (View v) {7 8 new Al Ertdialog.builder (dialogactivity.this) 9. Settitle ("delete"). Setmessage ("OK Delete specified data ") one by one. Setpositivebutton (" OK ", new Dialoginterface.onclickl Istener () {@Override15 public void OnClick (D Ialoginterface dialog,16 int which) {17 Toast.maketext (Getapplicationcontext (), 18 "OK", Toast.length_short) 19 . Show (); 20}21 }) 22 . Setnegativebutton ("Cancel", Dialoginterface.onclicklistener () {24 25 @Override26 public void OnClick (Dialoginterface dialog, int which) {28}29 }). Setcancelable (False). Show (); 30}31}); 32}
The effect is as follows:
Enter the dialog of the text box:
1 private void Inputdialog () {2 Button inputbtn = (button) Findviewbyid (R.id.inputdialog); 3 inputbtn.se Tonclicklistener (New Onclicklistener () {4 5 @Override 6 public void OnClick (View v) {7 TODO auto-generated Method Stub 8 final EditText et = new EditText (dialogactivity.this); 9 New Alertdialog.builder (dialogactivity.this) settitle ("Please enter Number") 11 . Setview (ET) Setpositivebutton ("OK", new Dialogin Terface. Onclicklistener () {@Override16 public void O Nclick (dialoginterface dialog,17 int which) {18 TODO auto-generated Method Stub19 Toast.maketext (Getapplicationcontex T (), 20 Et.gettext (), Toast.length_short). Show ( ) (}23}). Setnegativebutton ("Cancel", null) 24 . setcancelable (False). Show (); 25}26}); 27}
The effect is as follows:
List dialog:
private void Listdialogdemo () { button listbtn = (button) Findviewbyid (r.id.listdialog); Listbtn.setonclicklistener (New Onclicklistener () { @Override public void OnClick (View v) { final string[] Names = {"Cristiano Ronaldo", "J Luo", "H Luo"}; New Alertdialog.builder (Dialogactivity.this). Settitle ("List dialog box") . Setitems (Names, new Dialoginterface.onclicklistener () { @Override public void OnClick (Dialoginterface dialog, int which) { Toast.maketext (Dialogactivity.this, Names[which], toast.length_short). Show (); } }). Setnegativebutton ("Cancel", null). Show ();}} );
The effect is as follows:
Radio dialog:
1 private void Radiodialogdemo () {2 Button RadioButton = (button) Findviewbyid (R.id.radiodialog); 3 Radi Obutton.setonclicklistener (New Onclicklistener () {4 5 @Override 6 public void OnClick (View v) { 7 8 Final string[] names = {"Cristiano Ronaldo", "J Luo", "H Luo"}; 9 New Alertdialog.builder (dialogactivity.this) settitle ("List dialog box") 11 . Setsinglechoiceitems (names, 0,12 new Dialoginterface.onclicklistener () {13 14 @Override15 public void OnClick (Dialoginterface D ialog,16 int which) {Selectename = names[which];19}20}). s Etpositivebutton ("OK", new Dialoginterface.oncLicklistener () {@Override25 public void Oncl Ick (dialoginterface dialog,26 int which) {27 28 Toast.maketext (dialogactivity.this,29 selectename, Toast.length_ Short). Show (); 31}32 }). Setnegativebutton ("Cancel", null). Show (); 33}34}); 35}
The effect is as follows:
Multi-Select Dialog:
1 private void Checkdialogdemo () {2 Button checkbtn = (button) Findviewbyid (R.id.checkboxdialog); 3 Chec Kbtn.setonclicklistener (New Onclicklistener () {4 5 @Override 6 public void OnClick (View v) {7 Final string[] names = {"Cristiano Ronaldo", "J Luo", "H Luo"}; 8 final boolean[] selected = new boolean[] {true, false, true}; 9 New Alertdialog.builder (Dialogactivity.this) Setmultichoiceitems (11 names,12 selected,13 New Dialoginte Rface. Onmultichoiceclicklistener () {@Override16 PU Blic void OnClick (dialoginterface dialog,17 int which, Boolean isChecked) {18 }20}). SetpositivebuttOn ("OK", new Dialoginterface.onclicklistener () {23 24 @Override25 public void OnClick (Dialoginterface dialog,26 int which) {StringBuilder sb = new StringBuilder (28 "You have selected:"); (int i = 0; i < Names.le Ngth; i++) {if (Selected[i]) {31 Sb.append (Names[i]); 32}33}34 Toast.maketext (dialogactivity.this,35 sb.) ToString (), 1). Show ();}37}). Setnegativebutton ("Cancel" , null). Show (); 38 }39}); 40}
The effect is as follows:
Custom dialog:
1 private void Customdialogdemo () {2 final alertdialog dlg = new Alertdialog.builder (this). Create (); 3 dlg.show ( ); 4 Window window = Dlg.getwindow (); 5 Window.setcontentview (r.layout.diylayout); 6 ImageButton OK = ( ImageButton) Window.findviewbyid (R.id.btnok); 7 Ok.setonclicklistener (new View.onclicklistener () {8 9 @Override10 public void OnClick (View v) {11 Toast.maketext (Getapplicationcontext (), "closed", Toast.length_short). Show (); Dlg.dismiss (); 14 }15 });
Custom layouts:
1 <?xml version= "1.0" encoding= "Utf-8"?> 2 <relativelayout xmlns:android= "http://schemas.android.com/apk/ Res/android "3 android:layout_width=" match_parent "4 android:layout_height=" Match_parent "> 5 6 <ImageV Iew 7 android:id= "@+id/dialogimg" 8 android:layout_width= "wrap_content" 9 android:layout_height= "W Rap_content "android:layout_centerinparent=" true "one android:src=" @drawable/dialog_bg "/>12 ; TEXTVIEW14 android:layout_width= "wrap_content" android:layout_height= "Wrap_content" Android:la yout_alignleft= "@id/dialogimg" android:layout_aligntop= "@id/dialogimg" android:layout_marginleft= "50DP "Android:layout_margintop=" 60DP "android:text=" Custom Dialog "/>21 <imagebutton23 a Ndroid:id= "@+id/btnok" android:layout_width= "30DP" android:layout_height= "30DP" Android:layo ut_alignright= "@id/dialogimg" 27 android:layout_aligntop= "@id/dialogimg" android:layout_marginright= "15DP" Android:layout_mar gintop= "15DP" android:background= "@drawable/close_dialog"/>31 </RelativeLayout>
The effect is as follows:
Dialog dialog box in Android