Effect:
1. Simple dialog box
Alertdialog.builder builder=new Alertdialog.builder (alterdialogactivity.this); Builder . Settitle ("dialog box") . SetIcon (r.mipmap.ic_launcher_round). Setpositivebutton ("OK", null) . Setnegativebutton ("Cancel", null) . Setmessage ("Simple dialog Box"). Create () . Show ();
2. List dialog box
Alertdialog.builder builder=new Alertdialog.builder (alterdialogactivity.this); Final string[] mitems={"Item0", "Item1", "item2", "Item3", "Item4", "ITEM5", "Item6"}; Builder.settitle ("List dialog box") . Setitems (Mitems, New Dialoginterface.onclicklistener () { @Override public void OnClick (dialoginterface dialoginterface, int i) { toast.maketext (alterdialogactivity.this,mitems[i], Toast.length_short). Show (); } }) . Setpositivebutton ("OK", null) . Setnegativebutton ("Cancel", null). Create () . Show ();
3. Single-selection dialog box
Alertdialog.builder builder=new Alertdialog.builder (alterdialogactivity.this); Final string[] items={"item1", "item2", "Item3", "Item4"}; Builder.settitle ("Radio dialog Box") . SetIcon (R.mipmap.ic_launcher_round) . Setsinglechoiceitems (items,0,new Dialoginterface.onclicklistener () { @Override public void OnClick (dialoginterface dialoginterface, int i) { Toast.maketext (alterdialogactivity.this,items[i]+ "was clicked", Toast.length_short). Show (); singlechoise=i; } }) . Setpositivebutton ("OK", null) . Setnegativebutton ("Cancel", null). Create () . Show ();
4. Multi-Select dialog box
Alertdialog.builder builder=new Alertdialog.builder (alterdialogactivity.this); Final string[] items={"item1", "item2", "Item3", "Item4"}; Builder.settitle ("Multiple Selection Dialog"). SetIcon (R.mipmap.ic_launcher_round). Setmultichoiceitems (Items,ne W boolean[]{false,false,false,false},new Dialoginterface.onmultichoiceclicklistener () {@Override public void OnClick (dialoginterface dialoginterface, int i, Boolean b) {if (b) { Toast.maketext (alterdialogactivity.this,items[i]+ "was selected", Toast.length_short). Show (); }else{Toast.maketext (alterdialogactivity.this,items[i]+ "Unchecked", toast.length_short). Show (); }}). Setpositivebutton ("OK", null). Setnegativebutton ("Take NULL). Create (). Show ();
5. Customize the View dialog box
Alertdialog.builder builder=new Alertdialog.builder (alterdialogactivity.this); View view= Layoutinflater.from (this). Inflate (r.layout.alterdialot_img,null); builder// . Settitle ("Custom dialog box")// . SetIcon (R.mipmap.ic_launcher_round) . Setview (view) . Setcancelable (True) . Setnegativebutton ("Cancel", null). Create () . Show ();
Code:
Xml:
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "android:orientation=" vertical "android:layout_width=" match_parent "android:layout_height=" Match_parent "& Gt <button android:id= "@+id/button" android:layout_width= "match_parent" android:layout_height= "Wrap_c Ontent "android:onclick=" click "android:text=" dialog box "/> <button android:id=" @+id/button2 " Android:layout_width= "Match_parent" android:layout_height= "wrap_content" android:onclick= "click" A ndroid:text= "List dialog box"/> <button android:id= "@+id/button3" android:layout_width= "Match_parent" android:layout_height= "Wrap_content" android:onclick= "click" android:text= "Radio dialog box"/> <button Android:id= "@+id/button4" android:layout_width= "match_parent" android:layout_height= "Wrap_content" android:onclick= "click"android:text= "Multi-select dialog box"/> <button android:id= "@+id/button5" android:layout_width= "Match_parent" android:layout_height= "Wrap_content" android:onclick= "click" android:text= "Customize dialog box"/></linearlayout& Gt
Activity
public class Alterdialogactivity extends Appcompatactivity {//private alertdialog.builder Builder; private int singlechoise=0; @Override protected void OnCreate (@Nullable Bundle savedinstancestate) {super.oncreate (savedinstancestate); Supportrequestwindowfeature (Window.feature_no_title); Setcontentview (R.layout.act_alterlog);//Builder=new Alertdialog.builder (this); The public void Click (View v) {switch (V.getid ()) {r.id.button://Simple dialog box ShowDialog1 ( ); Break Case r.id.button2://list Box showDialog2 (); Break Case r.id.button3://Radio Box ShowDialog3 (); Break Case r.id.button4://Multi Box ShowDialog4 (); Break Case r.id.button5://Custom Contentview showDialog5 (); Break }} private void ShowDialog1 () {Alertdialog.builder Builder=new Alertdialog.builder (alterdialogactivity.this); Builder. Settitle ("dialog box"). SetIcon (r.mipmap.ic_launcher_round). setpositivebut ton ("OK", null). Setnegativebutton ("Cancel", null). Setmessage ("Simple dialog box"). Create () . Show (); } private void ShowDialog2 () {alertdialog.builder builder=new alertdialog.builder (alterdialogactivity.this); Final string[] mitems={"Item0", "Item1", "item2", "Item3", "Item4", "ITEM5", "Item6"}; Builder.settitle ("List dialog box"). Setitems (Mitems, New Dialoginterface.onclicklistener () {@Ove Rride public void OnClick (dialoginterface dialoginterface, int i) {Toast.makete XT (Alterdialogactivity.this,mitems[i],toast.length_short). Show (); }}). Setpositivebutton ("OK", null). Setnegativebutton ("Cancel", null) . CReate (). Show (); } private void ShowDialog3 () {alertdialog.builder builder=new alertdialog.builder (alterdialogactivity.this); Final string[] items={"item1", "item2", "Item3", "Item4"}; Builder.settitle ("Radio dialog Box"). SetIcon (R.mipmap.ic_launcher_round). Setsinglechoiceitems (items,0 , new Dialoginterface.onclicklistener () {@Override public void OnClick (DIALOGINTERFAC e dialoginterface, int i) {Toast.maketext (alterdialogactivity.this,items[i]+ "was clicked", Toast.length_sh ORT). Show (); Singlechoise=i; }}). Setpositivebutton ("OK", null). Setnegativebutton ("Cancel", null) . Create (). Show (); } private void ShowDialog4 () {alertdialog.builder builder=new alertdialog.builder (alterdialogactivity.this); Final string[] items={"item1", "item2", "Item3", "itEm4 "}; Builder.settitle ("Multiple Selection Dialog"). SetIcon (R.mipmap.ic_launcher_round). Setmultichoiceitems (Items,ne W boolean[]{false,false,false,false},new Dialoginterface.onmultichoiceclicklistener () {@Override public void OnClick (dialoginterface dialoginterface, int i, Boolean b) {if (b) { Toast.maketext (alterdialogactivity.this,items[i]+ "was selected", Toast.length_short). Show (); }else{Toast.maketext (alterdialogactivity.this,items[i]+ "Unchecked", toast.length_short). Show (); }}). Setpositivebutton ("OK", null). Setnegativebutton ("Take NULL). Create (). Show (); } private void ShowDialog5 () {alertdialog.builder builder=new alertdialog.builder (alterdialogactivity.this); View view= Layoutinflater.from (this). infLate (R.layout.alterdialot_img,null); builder//. Settitle ("Custom dialog box")//. SetIcon (r.mipmap.ic_launcher_round). Setview (view). Setcancelable (True). Setnegativebutton ("Cancel", null). Create () . Show (); }}
Introduction of Android Alterdialog