Android development-Use Dialog alertdialog

Source: Internet
Author: User

 

The dialog box is certainly familiar to everyone. The Windows operating system and IE browser often bring up dialog boxes. Most of these dialog boxes are modal synchronization dialogs. The so-called Mode means that the current pop-up dialog blocks its parent window. Before the dialog disappears, the parent window cannot get the focus, and the alert dialog box in the browser is like this; the so-called synchronization means that after the program runs the pop-up dialog, it stops at the current line of code. After the dialog processing is complete, the program continues to run.

In Android, dialog is asynchronous in mode. The so-called asynchronous dialog is like: you are talking to someone, but your brain is thinking about other things. When the android pop-up dialog box interacts with the user, the background execution thread continues to execute. At the same time, the dialog or managed is in Android. The so-called managed means that the system helps you maintain the dialog. They can be created once and called multiple times, so that you do not have to create a new dialog every time, saves memory and provides system performance.

Dialog is usually a small form in front of the activity. The activity loses focus, and the dialog interacts with the user. Then, it determines how to run the branch based on the user's choice or input program. There are several types of dialog in Android:

Alertdialog: a dialog box with 0, 1, 2, or 3 buttons. Its content can be text, checkbox, or radio listview, which is a frequently used dialog.

Progressdialog: A form contains the dialog of progress bar, which inherits alertdialog, so it has the same buttons as alertdialog.

Datepickerdialog: A dialog that provides the date selection function.

Timepickerdialog: A dialog that provides the time selection function.

You can also create a custom dialog and customize its layout. Let's take a look at the usage of various dialog. First, let's take a look at the simplest alert dialog.

Alertdialog is mainly used to prompt some text information, which is different from the alert method in JavaScript. In JavaScript, alert is a modal synchronous form, while alertdialog in Android is a modal asynchronous form. We use alertdialog. builder to create the alertdialog object. You can use this builder to complete the following tasks:

1. display the prompt information so that the user can select Yes or No.

2. Select an item from the listview provided by Dialog

3. Select multiple items from the listview provided by Dialog

4. display a progress bar

5. select one of the following options:

The steps for creating alertdialog are as follows:

1. Create a builder object

2. Set the dialog parameters to be created, such as the buttons and content to be displayed.

3. Set the callback function for the button (because the dialog in Android is asynchronous, the callback function is required)

4. Let the builder generate the dialog object based on the settings of the preceding steps.

5. Use the show () method to display the dialog.

Create an activity and a layout XML file:

Dialog. xml:

<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <linearlayout <br/> xmns: Android = "http://schemas.android.com/apk/res/android" <br/> Android: Orientation = "vertical" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "fill_parent"> </P> <p> <button <br/> Android: layout_width = "wrap_content" <br/> Android: layout_height = "wrap_content" <br/> Android: text = "display dialog" <br/> Android: onclick = "onshowdialogclick" <br/> </P> <p> </linearlayout> 

Dialogactivity. Java

Package COM. king. dialog </P> <p> Import android. app. activity; <br/> Import android. app. alertdialog; <br/> Import android. content. dialoginterface; <br/> Import android. OS. bundle; <br/> Import android. view. view; </P> <p> Import COM. android777.demo. uicontroller. r; </P> <p> public class demodialog extends activity {</P> <p> @ override <br/> protected void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); </P> <p> setcontentview (R. layout. DIALOG); </P> <p >}</P> <p> Public void onshowdialogclick (view v) {</P> <p> alertdialog. builder = new alertdialog. builder (this); <br/> builder. settitle ("test alertdialog"); <br/> builder. setpositivebutton ("OK", new emptylistener (); <br/> alertdialog ad = builder. create (); </P> <p> ad. show (); </P> <p >}< br/> // empty listening class <br/> private class emptylistener implements dialoginterface. onclicklistener {</P> <p> @ override <br/> Public void onclick (dialoginterface dialog, int which) {</P> <p >}</P> <p>} 

The preceding figure shows a simple dialog. Next, let's look at the dialog that is a little complex. What functions does it provide?

Public void onshowdialogclick (view v) {</P> <p> alertdialog. builder = new alertdialog. builder (this); <br/> emptylistener L = new emptylistener (); </P> <p> // set the dialog title <br/> builder. settitle ("test alertdialog"); <br/> // set the icon of the dialog <br/> builder. seticon (R. drawable. icon); <br/> // set the prompt information for the dialog <br/> builder. setmessage ("this is a prompt message, please select YES or NO"); <br/> builder. setpositivebutton ("yes", L); <br/> builder. setnegativebutton ("no", L); <br/> alertdialog ad = builder. create (); </P> <p> ad. show (); </P> <p >}< br/> 

 

You can also store single-or multiple-choice listview in dialog. First, define the items to be displayed. We put it in the resource file Res/values/arrays. xml:

Res/values/arrays. xml:

<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <resources> </P> <p> <string-array name = "select_days"> <br/> <item> Monday </item> <br/> <item> Tuesday </item> <br/> <item> Wednesday </item> <br/> <item> Thursday </item> <br/> <item> Friday </item> <br/> <item> Saturday </item> <br/> <item> Sunday </item> <br/> </string-array> </ p> <p> </resources> <br/> 

Public void onshowdialogclick (view v) {</P> <p> alertdialog. builder = new alertdialog. builder (this); <br/> emptylistener L = new emptylistener (); </P> <p> // set the dialog title <br/> builder. settitle ("test alertdialog"); <br/> // set the listview to be selected <br/> builder. setitems (R. array. select_days, new dialoginterface. onclicklistener () {</P> <p> @ override <br/> Public void onclick (dialoginterface dialog, int which) {<br/> // which is the selected position (based on 0) <br/> string [] items = getresources (). getstringarray (R. array. select_days); <br/> toast. maketext (demodialog. this, items [which], toast. length_long ). show (); <br/>}< br/>}); </P> <p> alertdialog ad = builder. create (); </P> <p> ad. show (); </P> <p>} 

To set a single choice, you only need to change the setitems method to setsinglechoiceitems. At the same time, one more parameter checkitem indicates that the first few options are selected by default. However, unlike setitems, setitems triggers the callback function when you select an item and closes the window. setsinglechoiceitems triggers the callback function when you select an item, however, the window is not closed. You need to set any of the three buttons to close the window.

Public void onshowdialogclick (view v) {</P> <p> alertdialog. builder = new alertdialog. builder (this); <br/> emptylistener L = new emptylistener (); </P> <p> // set the dialog title <br/> builder. settitle ("test alertdialog"); <br/> // set the listview to be selected <br/> builder. setsinglechoiceitems (R. array. select_days, 0, new dialoginterface. onclicklistener () {</P> <p> @ override <br/> Public void onclick (dialoginterface dialog, int which) {<br/> // which is the selected position (based on 0) <br/> string [] items = getresources (). getstringarray (R. array. select_days); <br/> toast. maketext (demodialog. this, items [which], toast. length_long ). show (); <br/>}< br/>}); </P> <p> builder. setpositivebutton ("OK", L); </P> <p> alertdialog ad = builder. create (); </P> <p> ad. show (); </P> <p>} 

 

You can see that when you click items in listview, the callback method is executed, but the form is not automatically closed.

The multi-choice box is similar to the single-choice box. The call method is setmultichoiceitems. However, the parameters are different. Because only one item is selected in a single choice, a number starting with 0 can be used to indicate the number of items to be selected, however, multiple items may be selected. Therefore, a Boolean array is used to store the status of each item. If this parameter is selected, the value is true. Otherwise, the value is false.

 

Date selection dialog box:

Calendar c = calendar. getinstance (); <br/> datepickerdialog ad = new datepickerdialog (this, <br/> New ondatesetlistener () {<br/> Public void ondateset (datepicker view, int year, <br/> int monthofyear, int dayofmonth) {<br/> // todo auto-generated method stub <br/> string date = string. valueof (year) + "-" <br/> + String. valueof (monthofyear + 1) + "-" <br/> + String. valueof (dayofmonth); <br/>}< br/>}, C. get (calendar. year), C. get (calendar. month), C <br/>. get (calendar. date); <br/> ad. show (); 

Time Selection dialog box:

Timepickerdialog ad = new timepickerdialog (this, <br/> New ontimesetlistener () {<br/> Public void ontimeset (timepicker view, int hourofday, <br/> int minute) {<br/> // todo auto-generated method stub <br/> string timer = ""; <br/> If (hourofday <10) {<br/> timer = "0" + String. valueof (hourofday) + ":" <br/> + String. valueof (minute); <br/>} else if (minute <10) {<br/> timer = string. valueof (hourofday) + ":" + "0" <br/> + String. valueof (minute); <br/>} else if (hourofday <10 & minute <10) {<br/> timer = "0" + String. valueof (hourofday) + ":" + "0" <br/> + String. valueof (minute); <br/>}else {<br/> timer = string. valueof (hourofday) + ":" <br/> + String. valueof (minute); <br/>}< br/>}, C. get (calendar. hour_of_day), C. get (calendar. minute), true); <br/> ad. show (); 

Dialog box to be entered:

Edittext EDT = new edittext (activitynotebooks. this); <br/> // the pop-up dialog box contains text <br/> EDT. settext (text); <br/> alertdialog ad = new alertdialog. builder (this ). settitle (<br/> r. string. wishnametitle ). setview (EDT ). setpositivebutton (<br/> r. string. OK, new onclicklistener () {<br/> Public void onclick (dialoginterface dialog, int which) {<br/> // todo auto-generated method stub <br/> string S = EDT. gettext (). tostring (); <br/>}< br/> }). setnegativebutton (R. string. cancle, null ). create (); <br/> ad. show (); <br/>} 


 

 

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.