Android-alertdialog the use of various dialog boxes
Last Update:2016-04-29
Source: Internet
Author: User
<span id="Label3"></p><p><p>Target Effect:</p></p><p><p></p></p><p><p>The program runs, displays several buttons of the graph one, click the button to display the dialog box of figure two to figure six respectively, click an item or button of the dialog, also show the corresponding toast Output.</p></p><p><p><br></p></p><p><p>The 1.activity_main.xml page contains five Buttons.</p></p><p><p>Activity_main.xml page:</p></p><pre code_snippet_id="1664746" snippet_file_name="blog_20160428_1_7613411" class="html" name="code"><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 "androi d: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= "radio Dialog box"/> <button android:id= "@+id/btncheck" A Ndroid:layout_width= "match_parent" android:layout_height= "wrap_content" android:layout_margintop= "110dp" android:text= "multi-select Dialog box"/> <button android:id= "@+id/btnlist" android:layout_width= "match_parent" android:layout_height= "wrap_content" android:layout_margintop= "160dp" android:text= "list Dialog box"/> <button android:id= "@+id/btnmy" Android:layout_width= "match_parent" android:layout_height= "wrap_content" android:layout_margintop= "210 DP "android:text=" custom dialog box "/></relativelayout></pre><br><br>2. Create a new Dialog.xml page as the layout page for the last custom dialog box. Dialog.xml page:<pre code_snippet_id="1664746" snippet_file_name="blog_20160428_2_3741953" class="html" name="code"><?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" and roid:orientation= "horizontal" > <edittext android:id= "@+id/edinput" Android:layout_widt h= "wrap_content" android:layout_height= "wrap_content" android:layout_weight= "2" > < requestfocus/> </EditText> <button android:id= "@+id/btnok" android:layout _width= "wrap_content" android:layout_height= "wrap_content" android:layout_weight= "1" Android oid:text= "ok"/> </LinearLayout> <imageview android:id= "@+id/ivpicture" android:layout_wid Th= "wrap_content" Android:layout_height= "280dp" android:src= "@drawable/white"/> <textview android:id= "@+id/textview1" A ndroid:layout_gravity= "center" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:text= "TextView"/></linearlayout></pre><br><br>3.mainactivity.java the popup and click events of the page Processing dialog box. Mainactivity.java page:<pre code_snippet_id="1664746" snippet_file_name="blog_20160428_3_1091995" class="java" name="code">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"};//single-choice list private string[] likelist={"basketball", " Football "," play Games "," listen to music "," see the movie "};//multi-select list private string[] itemlist={" project manager "," plan "," test "," Art "," programmer "};//list @overrideprotected Void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); setcontentview (r.layout.activity_main); GetId ();//get Control Idclick ();//button bind point Click 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 Sentinel event */private void click () {btnsure.setonclicklistener (this); btnradio.setonclicklistener (this); Btncheck.setonclicklistener (this), btnlist.setonclicklistener (this), btnmy.setonclicklistener (this);} @Overridepublic boolean oncreateoptionsmenu (menu menu) {getmenuinflater (). inflate (r.menu.main, menu); return true;} @Overridepublic 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 ();//multi-select Dialog Break;case r.id.btnlist: ShowDialog4 (); break;case r.id.btnmy:showdialog5 (); break;}} /* 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 (" Confirm dialog box prompt content ");//set content/* Add dialog box OK button and click event */builder.setpositivebutton ("ok", new Dialoginterface.onclicklistener () {@Overridepublic 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 () {@Overridepublic void OnClick (dialoginterface arg0, int arg1) {toast.maketext (mainactivity.this, "click cancel button", toast.length_short). show ();}); Alertdialog dialog=builder.create ();//get dialogdialog.show ();//show dialog}/* Radio dialog box */private void showDialog2 () { Alertdialog.builder builder=new alertdialog.builder (this); builder.settitle ("gender");//set caption Builder.seticon ( R.drawable.ic_launcher);//set icon///parameter a single-selection list text, parameter two is the default number of selected (-1 default unchecked), parameter three is to create listener */builder.setsinglechoiceitems ( Sexlist,-1,new dialoginterface.onclicklistener () {@Overridepublic void onClick (dialoginterface dialog, int Which) { String sex=sexlist[which]; Toast.maketext (mainactivity.this, "this person is gender" +sex, toast.length_short). show ();}); * Add dialog box Cancel button Click event */builder.setnegativebutton ("cancel", new Dialoginterface.onclicklistener () {@Overridepublic void OnClick (dialoginterface dialog, int Which) {dialog.dismiSS ();//close Dialog box}}); Alertdialog dialog=builder.create ();//get dialogdialog.show ();//show dialog}/* multi-select dialog */private void ShowDialog3 () { Alertdialog.builder builder=new alertdialog.builder (this); builder.settitle ("hobby");//set caption Builder.seticon ( R.drawable.ic_launcher)////set icon/* parameter same as Radio dialog box, The second parameter is not checked by default to null instead of -1*/builder.setmultichoiceitems (likelist,null, New Dialoginterface.onmultichoiceclicklistener () {@Overridepublic void onClick (dialoginterface dialog, int which, Boolean isChecked) {if (isChecked) {toast.maketext (mainactivity.this, "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 () {@Overridepublic void OnClick (dialoginterface dialog, int which) {dialog.dismiss ();//close dialog}}); Alertdialog dialog=builder.create ();//get dialogdialog.show ();//display Dialog box}/* List dialog */private void ShowDialog4 () { Alertdialog.builder builder=new Alertdialog.builder (this); builder.settitle ("department list");//set title Builder.seticon (r.drawable.ic_launcher);//set icon Builder.setitems (itemlist,new Dialoginterface.onclicklistener () {@Overridepublic void onClick (dialoginterface dialog, int Which) {toast.maketext ( mainactivity.this, "i clicked" +itemlist[which],toast.length_short). show ();}); Alertdialog dialog=builder.create ();//get dialogdialog.show ();//display Dialog box}/* custom dialog box */private void ShowDialog5 () { Layoutinflater Inflater=layoutinflater.from (this); View view=inflater.inflate (r.layout.dialog,null);//get Custom layouts Alertdialog.builder builder=new alertdialog.builder ( this); builder.settitle ("custom Dialog box");//set title Builder.seticon (r.drawable.ic_launcher);//set icon Builder.setview (view);// Set custom style Layout to dialog box Alertdialog dialog=builder.create ();//get dialogdialog.show ();//show dialog}}</pre><br><br>4. The target effect is in Operation.<p><p><br></p></p><p><p>Android-alertdialog the use of various dialog boxes</p></p></span>