Android Getting Started-alertdialog dialog box

Source: Internet
Author: User

Common dialog boxes are grouped into message prompt dialog boxes, confirmation dialogs, List dialogs, Radio dialog boxes, multi-select dialogs, and custom dialogs.

Dialog boxes can block the current UI thread, often used to exit acknowledgments, and so on.

Here the main steps can be summed up as:
1. Create a Alertdialog.builder object;

2. Call the Settitle () or Setcustomtitle () method to set the caption;

3. Call the SetIcon () settings icon;

4. Call Setpositivebutton (), Setnegativebutton () or Setneturalbutton () to add a button;

5. Call the Create () method of Alertdialog.builder for creating the Alertdialog object;

6. Call the show () method of Alertdialog.builder to display the dialog box.

In this section, the main use is the builder pattern in design mode, which is provided to builder and then assembled into a complete dialog box.

① Display a dialog box for a prompt message

                                        public Boolean Onitemlongclick (adapterview<?> parent, view view, final int p Osition, long id) {New Alertdialog.buil                                            Der (This). SetIcon (R.drawable.ic_4)                                            . Settitle ("hint"). Setmessage ("This is a hint message") Processing a dialog box is often handled in an event, so use an inner class to do so at this time. Setpositivebutton ("OK", new Dialogint Erface.                                                Onclicklistener () {@Override public void OnClick (Dialoginterface dialog, int which) {//Note here the This must Need to be modified, because it is in the inner class, so the direct use of this refers to this inner class, so it is necessary to modify Toast.maketext (Mainactivity.thi S, "clicked Confirm", Toast.                                                Length_short). Show ();                                            }}). Show ();                                        return true; }

Attributes such as icons, captions, and cue information are set.

② Confirmation dialog box

         public Boolean Onitemlongclick (adapterview<?> parent, view view, final int position, long ID) {New Alertdialog.builder (this). setic On (r.drawable.ic_4). Settitle ("hint")//Can set Nu ll, which means nothing is done after the click, no subsequent processing//Just makes the dialog box disappear. Setnega                                        Tivebutton ("Cancel", NULL)//processing of dialogs is often handled in an event, so this is done using an inner class                                            . Setpositivebutton ("OK", new Dialoginterface.onclicklistener () {                                                @Override public void OnClick (Dialoginterface dialog, int which) {                                                Note that this must be modified because it is in the inner class, so direct use of this refers to this inner class, so you need to modify Toast.maKetext (Mainactivity.this, "clicked Confirm", Toast.length_short). Show (); You can also use finish () to end the current activity's life cycle, become invisible, and then use the activity's resources//if the current activity                                                is the main interface, then the activity stack becomes empty, finish ();                                            If the call to exit () is//system.exit (0);                                }}). Show ();                            return true; }

The icon and title are added here.

The main change is the addition of the Setnegativebutton () method and the Setpositivebutton () method, which are used to click Confirm or Cancel.

③ List dialog box

public Boolean Onitemlongclick (adapterview<?> parent, view view, final int position, long ID) {Final string[] arr = {"Water can carry a boat, or it may be rowing." "Don't always want to make big news!" "What country in the West have I not been to?"                                        "," Too Young,too simple! "};                                                New Alertdialog.builder (this). SetIcon (R.drawable.ic_4) . Settitle ("hint"). Setitems (arr, new Dialoginterf Ace.                                                    Onclicklistener () {@Override public void OnClick (Dialoginterface dialog, int which) {Toast.maketext (Mainactivity.this, "You have selected:" + Arr[which], toast.length_short). Show ();                                                    }                                                })                                                    . Setpositivebutton ("OK", new Dialoginterface.onclicklistener () { @Override public void OnClick (Dialoginterface di                                                Alog, int which) {}})                                                    . Setnegativebutton ("Cancel", new Dialoginterface.onclicklistener () { @Override public void OnClick (                                                Dialoginterface dialog, int which) {}                                        }). Show ();                     return true;               } 

A character array is defined first, and the character array is bound to it in the onclick ().

  

Android Getting Started-alertdialog dialog box

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.