Android UI Learning Dialog

Source: Internet
Author: User

This section learns Diaolog. In life dialog includes: three styles (Single-selection dialog box, multi-Select dialog box, OK Cancel dialog box)

Let's one analysis:


To determine the Cancel dialog box:

public void Click1 (View v)    {    Alertdialog.builder builder = new Builder (mainactivity.this);        Set icon    Builder.seticon (Android. R.drawable.alert_light_frame);    Set    the title builder.settitle ("hint");    Set body    builder.setmessage ("Do you want to quit the game?") ");    Set OK button    Builder.setpositivebutton ("OK", new Onclicklistener () {@Overridepublic void OnClick (dialoginterface arg0, int arg1) {///OK after callback function Toast.maketext (Mainactivity.this, "exit", Toast.length_short). Show ();});    Set Cancel button    Builder.setnegativebutton ("Cancel", new Onclicklistener () {@Overridepublic void OnClick (dialoginterface arg0, int arg1) {//Cancel after callback function Toast.maketext (Mainactivity.this, "Cancel", Toast.length_short). Show ();});        Display dialog    Alertdialog dialog = Builder.create ();    Dialog.show ();    }
Display effect:


Single-selection dialog box:

public void Click2 (View v)    {    Alertdialog.builder builder = new Builder (mainactivity.this);    Builder.settitle ("Please select Gender");        Final string[] items = new string[]{    "male",    "female"    };        1 means: The default is not selected, 0 for male, 1 for female    builder.setsinglechoiceitems (items, 1, new Onclicklistener () {    //which: The delegate is to select that option @overridepublic void OnClick (dialoginterface dialog, int which) {Toast.maketext (Mainactivity.this, items[ Which] + "is selected! ", Toast.length_short). Show ();d Ialog.dismiss ();});        Builder.show ();    }

The results shown are:

Multi-Select dialog box:

public void Click3 (View v) {alertdialog.builder builder = new Builder (mainactivity.this);    Builder.settitle ("Please select your preferred type");        Final string[] items = new string[]{"Gentle", "Sexy", "hot", "Nanny", "Lori Type",};    Initial value: True to indicate check, false to uncheck final boolean[] CheckedItems = new boolean[]{True, True, False, true, true,        }; Builder.setmultichoiceitems (items, CheckedItems, new Onmultichoiceclicklistener () {//which: The subscript of the entry clicked by the user//ischecked: Whether the user selects the entry or cancels the entry @overridepublic void OnClick (dialoginterface arg0, int which, Boolean isChecked) {Checkeditems[which] =    isChecked;}        }); Builder.setpositivebutton ("OK", new Onclicklistener () {@Overridepublic void OnClick (dialoginterface dialog, int which) {//encapsulation displays string text = ""; for (int i = 0; i < items.length; i++) {text + = Checkeditems[i]? Items[i] + ";": "";}        Toast.maketext (mainactivity.this, Text, Toast.length_short). Show ();d Ialog.dismiss ();});    Builder.show (); }

Display effect:


About dialog, it's here.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Android UI Learning Dialog

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.