A dialog box is a pop-up window in a program run. For example, when a user wants to delete a contact, it pops up a
dialog box to let the user confirm that they really want to delete.
Android system provides four dialog boxes: Warning dialog (Alertdialog), progress Dialog (ProgressDialog), Date selection dialog (Datepickerdialog) and Time Selection dialog (Timepickerdialog)
warning dialog box (alertdialog)
Alertdialog is a prompt window that asks the user to make a selection. The dialog box typically has several selection buttons, title information, and hints.
Creating a dialog box in a program requires the following steps:
1. Obtain the static internal class Builder object of Alertdialog, which is created by the class to create a dialog box.
2. Set the dialog's caption, Button, and the event that the button will respond to by Buidler object.
3. Call the builder's Createo method to create a dialog box.
4. Call Alertdialog's Showo method to display the dialog box.
The instance code is as follows:
public class mainactivity extends activity { @Override protected void oncreate (bundle savedinstancestate) { super.oncreate (savedinstancestate); setcontentview (R.layout.activity_main); final textview mytext= (TextView) This.findviewbyid (R.ID.TEXT1); Final alertdialog.builder builder=new alertdialog.builder (this);//Create Builder Object button btn1= (Button) This.findviewbyid (r.id.btn_main1); btn1.setonclicklistener (New onclicklistener () { @Override public&Nbsp;void onclick (view arg0) { // TODO Auto-generated method stub builder.setmessage ("Are you sure! ") . Setpositivebutton ("OK", New dialoginterface.onclicklistener () { @Override Public void onclick (DIALOGINTERFACE ARG0, INT ARG1) { // todo auto-generated method stub mytext.settext ("Success"); } }). Setnegativebutton ("Cancel", New dialoginterface.onclicklistener () { @Override public void onclick (DIALOGINTERFACE ARG0, INT ARG1) { // todo auto-generated method stub mytext.settext ("Cancel"); } }); alertdialog ad= Builder.create (); ad.show (); } }); }}
The effect is as follows, after clicking the key:
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/59/CB/wKioL1Tl1qCikC5TAAB0JEaEOgQ658.jpg "title=" 1.png " alt= "Wkiol1tl1qcikc5taab0jeaeogq658.jpg"/>
Another dialog box with the following code:
public class mainactivity extends activity { private static final int menu1=menu.first; private static final int menu2=menu.first+1; @Override protected void OnCreate (bundle savedinstancestate) { super.oncreate ( Savedinstancestate); setcontentview (R.layout.activity_main); final textview mytext= (TextView) this.findViewById ( R.ID.TEXT1); final alertdialog.builder builder=new alertdialog.builder (This); final string[] message={"--------", "*******", "++++++++"}; button btn1= ( Button) This.findviewbyid (r.id.btn_main1); btn1.setonclicklistener (New OnClickListener () { @Override public void onclick (View arg0) { // todo Auto-generated method stub //Two different styles of dialog boxes builder.settitle ("Please choose ...") .setsinglechoiceitems (message, -1, new dialoginterface.onclicklistener () { &nbsP;//.setitems (Message,new dialoginterface.onclicklistener () { @Override Public void onclick (DIALOGINTERFACE ARG0, INT ARG1) { // TODO Auto-generated method stub Mytext.settext (Message[arg1]); } }); alertdialog ad =builder.create (); ad.show (); } }); }}
The above two modes are as follows:
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/59/CB/wKioL1Tl2r3QgEQcAAC08I9GkZs362.jpg "title=" 1.png " alt= "wkiol1tl2r3qgeqcaac08i9gkzs362.jpg"/> 650) this.width=650; "Src=" http://s3.51cto.com/wyfs02/M00/59/CF/ Wkiom1tl2cba1ywpaadlhrqse5o529.jpg "title=" 2.png "alt=" Wkiom1tl2cba1ywpaadlhrqse5o529.jpg "/>
This article is from the "Useless Uncle" blog, please be sure to keep this source http://aslonely.blog.51cto.com/6552465/1614888
Android dialog box (i)