Adrnoid Development Series (25): Use alertdialog to create various types of dialog boxes

Source: Internet
Author: User

Alertdialog can generate various content dialog boxes, but each dialog box has this structure:


Similar to the following:


This is just the simplest dialog box.


Let's take a look at the steps required to create a dialog box:

1. Create an alertdialog. Builder object

2. Call the settitle () or setcustomtitle () method of alertdialog. builder to set the title.

3. Call the seticon () method setting icon of alertdialog. builder.

4. Call other settings to set the title.

5. Call setpositivebutton (), setnegativebutton (), or setneutralbutton () of alertdialog. builder to add multiple buttons.

6. Call the CREATE () method to create the alertdialog object, and then call the show () method of the alertdialog object to display the dialog box.


Create an android project and write main. xml:

<? XML version = "1.0" encoding = "UTF-8"?> <Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" Android: Orientation = "vertical" Android: layout_width = "match_parent" Android: layout_height = "match_parent" Android: gravity = "center_horizontal">! -- Display a common text edit box component --> <edittext Android: Id = "@ + ID/Show" Android: layout_width = "match_parent" Android: layout_height = "wrap_content" Android: editable = "false"/> <! -- Define a common button component --> <buttonandroid: layout_width = "match_parent" Android: layout_height = "wrap_content" Android: text = "simple dialog box" Android: onclick = "simple"/> <! -- Define a common button component --> <buttonandroid: layout_width = "match_parent" Android: layout_height = "wrap_content" Android: text = "simple list items dialog box" Android: onclick = "simplelist"/> <! -- Define a common button component --> <buttonandroid: layout_width = "match_parent" Android: layout_height = "wrap_content" Android: text = "Single-choice list items dialog box" Android: onclick = "singlechoice"/> <! -- Define a common button component --> <buttonandroid: layout_width = "match_parent" Android: layout_height = "wrap_content" Android: text = "Multi-choice list items dialog box" Android: onclick = "multichoice"/> <! -- Define a common button component --> <buttonandroid: layout_width = "match_parent" Android: layout_height = "wrap_content" Android: text = "custom list items dialog box" Android: onclick = "customlist"/> <! -- Define a common button component --> <buttonandroid: layout_width = "match_parent" Android: layout_height = "wrap_content" Android: text = "Custom view dialog box" Android: onclick = "customview"/> </linearlayout>
6 buttons and a text display box are defined, and the onclick attribute is set.


Next, we need to write the main interface's Java code: alertdialogtest. Java

Package Org. crazyit. ui; import android. app. activity; import android. app. alertdialog; import android. content. dialoginterface; import android. content. dialoginterface. onclicklistener; import android. OS. bundle; import android. view. view; import android. widget. arrayadapter; import android. widget. tablelayout; import android. widget. textview; public class alertdialogtest extends activity {textview show; string [] items = New string [] {"Crazy Java handouts", "Crazy Ajax handouts", "lightweight Java EE Enterprise Application practices", "crazy android handouts "}; @ overridepublic void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); show = (textview) findviewbyid (R. id. show);} public void simple (View Source) {alertdialog. builder = new alertdialog. builder (this) // set the dialog box title. settitle ("this is the title of the dialog box") // set the icon. seticon (R. drawable. tools ). setme Ssage ("this is the content of the dialog box"); // It is alertdialog. add the [OK] button setpositivebutton (builder); // It is alertdialog. setnegativebutton (builder) added to builder ). create (). show ();} public void simplelist (View Source) {alertdialog. builder = new alertdialog. builder (this) // set the dialog box title. settitle ("simple list items dialog box") // sets the icon. seticon (R. drawable. tools) // set the content of a simple list item. setitems (items, new onclicklistener () {@ overridepublic void onclick (dialoginte Rface dialog, int which) {Show. settext ("You selected" + items [which] + "") ;}}); // It is alertdialog. add the [OK] button setpositivebutton (builder); // It is alertdialog. setnegativebutton (builder) added to builder ). create (). show ();} public void singlechoice (View Source) {alertdialog. builder = new alertdialog. builder (this) // set the dialog box title. settitle ("Single-choice list items dialog box") // sets the icon. seticon (R. drawable. tools) // set a single-choice list item. The second item (Index 1) is selected by default ). setsingle Choiceitems (items, 1, new onclicklistener () {@ overridepublic void onclick (dialoginterface dialog, int which) {Show. settext ("You selected" + items [which] + "") ;}}); // It is alertdialog. add the [OK] button setpositivebutton (builder); // It is alertdialog. setnegativebutton (builder) added to builder ). create (). show ();} public void multichoice (View Source) {alertdialog. builder = new alertdialog. builder (this) // set the dialog box title. setti TLE ("Multi-choice list items dialog box") // sets the icon. seticon (R. drawable. tools) // set the multiple-choice list items. Set to check 2nd items and 4th items. setmultichoiceitems (items, new Boolean [] {false, true, false, true}, null); // alertdialog. add the [OK] button setpositivebutton (builder); // It is alertdialog. setnegativebutton (builder) added to builder ). create (). show ();} public void customlist (View Source) {alertdialog. builder = new alertdialog. builder (this) // set the dialog box title. settitle ("custom list Dialog Box ") // set the icon. seticon (R. drawable. tools) // set custom list items. setadapter (New arrayadapter <string> (this, R. layout. array_item, items), null); // alertdialog. add the [OK] button setpositivebutton (builder); // It is alertdialog. setnegativebutton (builder) added to builder ). create (). show ();} public void customview (View Source) {// load/RES/layout/login. XML interface layout tablelayout loginform = (tablelayout) getlayoutinflater (). inflate (R. layo Ut. login, null); New alertdialog. builder (this) // set the icon in the dialog box. seticon (R. drawable. tools) // set the title of the dialog box. settitle ("Custom view dialog box") // set the view object displayed in the dialog box. setview (loginform) // set a "OK" button for the dialog box. setpositivebutton ("login", new onclicklistener () {@ overridepublic void onclick (dialoginterface dialog, int which) {// You can execute logon here }}) // set a "cancel" button for the dialog box. setnegativebutton ("cancel", new onclicklistener () {@ overridepublic void onclick (dialoginterf Ace dialog, int which) {// cancel login, do not do anything .}}) // Create and display the dialog box. create (). show ();} private alertdialog. builder setpositivebutton (alertdialog. builder) {// call the setpositivebutton method to add the confirmation button return builder. setpositivebutton ("OK", new onclicklistener () {@ overridepublic void onclick (dialoginterface dialog, int which) {Show. settext ("click OK! ") ;}}) ;}Private alertdialog. builder setnegativebutton (alertdialog. builder) {// call the setnegativebutton method to add the cancel button return builder. setnegativebutton ("cancel", new onclicklistener () {@ overridepublic void onclick (dialoginterface dialog, int which) {Show. settext ("click the cancel button! ");}});}}
Here, the fifth and sixth buttons use two styles: array_item.xml and login. xml.


Let's take a look at their content:

Array_item.xml:

<?xml version="1.0" encoding="utf-8"?><TextView xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@+id/TextView"    android:textColor="#f0f"    android:textSize="30dp"    android:shadowColor="#ff0"    android:shadowRadius="2"    android:shadowDx="5"    android:shadowDy="5"android:layout_width="match_parent" android:layout_height="wrap_content" />

Login. xml:

<? XML version = "1.0" encoding = "UTF-8"?> <Tablelayout xmlns: Android = "http://schemas.android.com/apk/res/android" Android: Id = "@ + ID/loginform" Android: Orientation = "vertical" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent"> <tablerow> <textviewandroid: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: text = "username:" Android: textsize = "10pt"/> <! -- Enter the text box of the user name --> <edittextandroid: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: hint = "Please enter the Logon account" Android: selectallonfocus = "true"/> </tablerow> <textviewandroid: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: text = "Password:" Android: textsize = "10pt"/> <! -- Enter the password text box --> <edittextandroid: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: hint = "enter the password" Android: password = "true"/> </tablerow> <textviewandroid: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: text = "phone number:" Android: textsize = "10pt"/> <! -- Enter the phone number text box --> <edittextandroid: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: hint = "Please enter your phone number" Android: selectallonfocus = "true" Android: phonenumber = "true"/> </tablerow> </tablelayout>

Alertdialog can be used to create dialogs of different styles.

In addition, we can use the OK button to transmit data to another interface through intent.


Adrnoid Development Series (25): Use alertdialog to create various types of dialog boxes

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.