This section describes the basic components of Android: alertdialog in the dialog box.
API:
| Java. Lang. Object |
| Bytes |
Android. App. alertdialog. Builder |
To create a dialog box using alertdialog. Builder, you need to understand the following methods:
- Settitle: Set the title for the dialog box
- Seticon: Set the icon for the dialog box
- Setmessage: Set content for the dialog box
- Setview: Set a custom style for the dialog box
- Setitems: Set a list to be displayed in the dialog box. It is generally used to display several commands.
- Setmultichoiceitems: used to set a series of check boxes in the dialog box.
- Setneutralbutton:
- Setpositivebutton: add the "yes" button to the dialog box.
- Setnegativebutton: add the "no" button in the dialog box
- Create: Create dialog box
- Show: Show dialog box
Next let's take a look at the simplest dialog box.
This dialog box is just a simple display of the content. Use the default icon without a button. leave it blank and paste the Code:
New alertdialog. Builder (lesson_01_pic.this). settitle ("prompt title"). setmessage ("this is the prompt content"). Show ();
(Lesson_02_dia is the class name. Replace it with your own !!)
Next we will add a button for this dialog box. The effect is as follows:
Code:
New alertdialog. builder (lesson_01_pic.this) <br/>. settitle ("this is the title") <br/>. setmessage ("this is the prompt content") <br/>. setpositivebutton ("OK", <br/> New dialoginterface. onclicklistener () {<br/> Public void onclick (dialoginterface, int I) {<br/> // button event <br/> toast. maketext (lesson_01_pic.this, "OK", toast. length_long ). show (); <br/>}< br/> }). show ();
When adding a button, you must specify a listener for the button.
Next, we will modify an icon and add two buttons to display multiple options at the same time. Let's first look at the effect:
Code:
Package COM. yfz; <br/> Import android. app. activity; <br/> Import android. app. alertdialog; <br/> Import android. app. dialog; <br/> Import android. content. dialoginterface; <br/> Import android. content. dialoginterface. onclicklistener; <br/> Import android. content. dialoginterface. onmultichoiceclicklistener; <br/> Import android. OS. bundle; <br/> Import android. view. view; <br/> Import android. widget. button; <br/> I Mport android. widget. toast; <br/> public class lesson_02_dia extends activity {<br/>/** called when the activity is first created. */<br/> @ override <br/> Public void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); <br/> setcontentview (R. layout. main); </P> <p> button = (button) findviewbyid (R. id. b01); <br/> button. settext ("dialog box"); <br/> button. setonclicklistene R (New button. onclicklistener () {<br/> @ override <br/> Public void onclick (view V) {<br/> // option array <br/> string [] choices = {"Facebook", "Twitter" };< br/> // check the array, corresponds to the option <br/> Boolean [] chsbool = {true, false }; <br/> // dialog box containing multiple options and check boxes <br/> alertdialog dialog = new alertdialog. builder (lesson_02_dia.this) <br/>. seticon (Android. r. drawable. btn_star_big_on) <br/>. settitle ("investigation") <br/>. setmultichoiceitems (choic Es, chsbool, multiclick) <br/>. setpositivebutton ("yes", onclick) <br/>. setnegativebutton ("no", onclick ). create (); <br/> dialog. show (); <br/>}</P> <p> }); <br/>}</P> <p>/** <br/> * check box event listener <br/> */<br/> onmultichoiceclicklistener multiclick = new onmultichoiceclicklistener () {<br/> @ override <br/> Public void onclick (dialoginterface dialog, int which, Boolean ischecked) {<br/> toast. maketext (Le Sson_02_dia.this, "Number" + (which + 1) + ", selected Result:" + ischecked, toast. length_short ). show (); <br/>}</P> <p> }; </P> <p>/** <br/> * click the event listener button in the dialog box <br/> */<br/> onclicklistener onclick = new onclicklistener () {<br/> @ override <br/> Public void onclick (dialoginterface dialog, int which) {<br/> switch (which) {<br/> case dialog. button_negative: <br/> toast. maketext (lesson_02_dia.this, "No .. ", <br/> toast. length_long ). sho W (); <br/> break; <br/> case dialog. button_neutral: <br/> toast. maketext (lesson_02_dia.this, "I don't know. ", <br/> toast. length_long ). show (); <br/> break; <br/> case dialog. button_positive: <br/> toast. maketext (lesson_02_dia.this, "Yes !! ", <Br/> toast. length_long ). show (); <br/> break; <br/>}< br/>}; <br/>}
The description has been written in a comment.
Next we will introduce a common style,
Code:
@ Override <br/> Public void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); <br/> setcontentview (R. layout. main); </P> <p> button = (button) findviewbyid (R. id. b01); <br/> button. settext ("dialog box"); <br/> button. setonclicklistener (New button. onclicklistener () {<br/> @ override <br/> Public void onclick (view V) {<br/> // option array <br/> string [] choices = {"Sina Weibo", "campus", "nearby "}; <br/> // dialog box containing multiple options <br/> alertdialog dialog = new alertdialog. builder (lesson_02_dia.this) <br/>. seticon (Android. r. drawable. btn_star) <br/>. settitle ("share") <br/>. setitems (choices, onselect ). create (); <br/> dialog. show (); <br/>}< br/> }); <br/>}</P> <p>/** <br/> * option event listener <br/> */<br/> onclicklistener onselect = new onclicklistener () {<br/> @ override <br/> Public void onclick (dialoginterface dialog, int which) {<br/> // todo auto-generated method stub <br/> switch (which) {<br/> case 0: <br/> toast. maketext (lesson_02_dia.this, "you selected Sina Weibo. ", toast. length_short ). show (); <br/> break; <br/> case 1: <br/> toast. maketext (lesson_02_dia.this, "you selected school", toast. length_short ). show (); <br/> break; <br/> case 2: <br/> toast. maketext (lesson_02_dia.this, "you selected the Street", toast. length_short ). show (); <br/> break; <br/>}</P> <p> };
Now, write it here today, and write it another day. If you create a login interface in the pop-up box.
Continue to supplement... first...
Page login. xml: the example is easy to write, and you can complete and modify the layout yourself.
<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <tablelayout <br/> Android: Id = "@ + ID/widget36" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "fill_parent" <br/> Android: Orientation = "vertical" <br/> xmlns: android = "http://schemas.android.com/apk/res/android" <br/> <textview <br/> Android: Id = "@ + ID/widget37" <br/> Android: layout_width = "wrap_content" <br/> Android: layout_height = "wrap_content" <br/> Android: text = "username: "<br/> </textview> <br/> <edittext <br/> Android: id = "@ + ID/widget38" <br/> Android: layout_width = "wrap_content" <br/> Android: layout_height = "wrap_content" <br/> Android: TEXT = "" <br/> Android: textsize = "18sp" <br/> </edittext> <br/> <textview <br/> Android: id = "@ + ID/widget39" <br/> Android: layout_width = "wrap_content" <br/> Android: layout_height = "wrap_content" <br/> Android: TEXT = "Password:" <br/> </textview> <br/> <edittext <br/> Android: id = "@ + ID/widget40" <br/> Android: layout_width = "wrap_content" <br/> Android: layout_height = "wrap_content" <br/> Android: TEXT = "" <br/> Android: textsize = "18sp" <br/> </edittext> <br/> </tablelayout> <br/>
Code: (relatively simple)
Layoutinflater factory = layoutinflater. from (lesson_02_dia.this); <br/> // get custom dialog box <br/> View view = factory. inflate (R. layout. login, null); </P> <p> alertdialog dialog02 = new alertdialog. builder (lesson_02_dia.this) <br/>. seticon (Android. r. drawable. btn_star) <br/>. settitle ("login") <br/>. setview (View) <br/>. setpositivebutton ("yes", onclick) <br/>. setnegativebutton ("no", onclick ). create (); <br/> dialow.2.show ();
If you have any questions, please contact us.