Android UI Series--dialog box (i) (Alertdialog,timepickerdialog,datepickerdialog,progressdialog)

Source: Internet
Author: User
Tags gettext

I. Dialog INTRODUCTION

dialog is a dialog box that pops up on the screen to allow the user to make a choice, or to enter additional information, a dialog box that is not stained with our entire screen, and is typically used in model events to require a user to make a decision before continuing.

The dialog class is the base class for the dialog dialog box, but we should avoid using this class directly to instantiate a dialog dialog box, and we should use its subclasses to get a dialog box.

Dialog has a lot of subclasses implemented, so we're going to define a dialog box, use its subclasses to instantiate one, instead of just using the dialog parent class to construct

Java.lang.Object   ?     Android.app.DialogKnown Direct subclasses (directly subclass) Alertdialog, Characterpickerdialog, Mediaroutechooserdialog, Mediaroutecontrollerdialog, Presentationknown Indirect subclasses (indirect subclass) Datepickerdialog, ProgressDialog, Timepickerdialog

Two. Alertdialog

Alertdialog is a direct subclass of dialog, and we typically instantiate a dialog object directly with Alertdialog.builder.

Alertdialog mainly includes warning box, radio box, multi box, drop-down list popup, custom popup (Create a popup screen yourself)

The interface design XML file (Main_activity.xml) is as follows:

<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"Android:paddingbottom= "@dimen/activity_vertical_margin"Android:paddingleft= "@dimen/activity_horizontal_margin"Android:paddingright= "@dimen/activity_horizontal_margin"Android:paddingtop= "@dimen/activity_vertical_margin"Tools:context=". Mainactivity "android:orientation= "Vertical" > <Button android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Create dialog box"Android:onclick= "Click1"/> <Button android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Create a radio box"Android:onclick= "Click2"/> <Button android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Create multi-Marquee"Android:onclick= "Click3"/> <Button android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Create drop-down box/and message different"Android:onclick= "Click4"/> <Button android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Get user name and password"Android:onclick= "Click5"/></linearlayout>

The code for the custom XML interface (dialog.xml) file is as follows:

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "    android:layout_width=" match_parent "    android:layout_height=" match_parent "    android:o rientation= "vertical" >        <edittext      android:id= "@+id/username" android:layout_width= "Match_       Parent "     android:layout_height=" wrap_content "     android:hint=" Please enter user name        />         <edittext      Android:id= "@+id/password"       android:layout_width= "match_parent"     android:layout_height= "Wrap_content"     android:hint= "Please enter password"        />    </LinearLayout>

The Java file code is as follows:

Package Com.wyy.dialog;import Android.os.bundle;import Android.app.activity;import android.app.alertdialog;import Android.app.alertdialog.builder;import Android.app.dialog;import Android.content.dialoginterface;import Android.content.dialoginterface.onclicklistener;import Android.content.dialoginterface.onmultichoiceclicklistener;import Android.text.style.bulletspan;import Android.view.layoutinflater;import Android.view.menu;import Android.view.view;import Android.widget.EditText; Import Android.widget.toast;public class Mainactivity extends Activity {@Overrideprotected void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main);} public void Click1 (View v) {Alertdialog.builder builder=new Builder (this);Builder.seticon(Trim_memory_background);Builder.settitle("Please select:");Builder.setmessage("Work is important or life is important?"); Builder.Setpositivebutton("Work", new Onclicklistener () {@Overridepublic void OnClick (dialoginterface dialog, int which) {//TODO auto-generated Meth Od stubtoast.maketext (mainactivity.this, "isn't work for a better life?" ", 0). Show ();}}); Builder.Setnegativebutton("Life", new Onclicklistener () {@Overridepublic void OnClick (dialoginterface dialog, int which) {//TODO auto-generated Meth Od stubtoast.maketext (mainactivity.this, "work may make you feel more in existence", 0). Show ();}} ); builder.Setneutralbutton("Ignore", new Onclicklistener () {@Overridepublic void OnClick (dialoginterface dialog, int which) {//TODO auto-generated met Hod Stubtoast.maketext (mainactivity.this, "neutral" +which, 0). Show ();});Alertdialog ad=builder.create (); Ad.show ();}

public void Click2 (View v) {alertdialog.builder builder=new Builder (this); Builder.settitle ("Please select your gender");final string[] items=new string[]{"Male", "female", "other"};builder.Setsinglechoiceitems(Items, -1,new Onclicklistener () {@Overridepublic void OnClick (dialoginterface dialog, int which) {//TODO auto-generated Method Stubtoast.maketext (Mainactivity.this, "You have selected" +Items[which], 0). Show ();d Ialog.dismiss (); }} );builder.show ();}public void Click3 (View v) {alertdialog.builder builder=new Builder (this); Builder.settitle ("Who is the most Handsome star");
/**                 * The first parameter specifies the set of data sets that we want to display for the next Lado box.                 * The second parameter represents which options are selected and, if NULL, indicates that none is selected, and if you want to specify which multi-select option box is selected,                 * A boolean[] array needs to be passed in with the same length as the first parameter, for example {True, False, False, true};                 * The third parameter binds a listener to each of the multiple options                 */
final string[] items=new string[]{"Hu", "Huojianhua", "Shen", "Yang Yang"};
Final boolean[] Checkeditems=new boolean[]{true,false,true,false};
Builder.Setmultichoiceitems(Items, CheckedItems, new Onmultichoiceclicklistener () {@Overridepublic void OnClick (dialoginterface dialog, int which, Boolean isChecked) {//TODO auto-generated method stubcheckeditems[which]=ischecked;}}); Builder.setpositivebutton ("OK", new Onclicklistener () {@Overridepublic void OnClick (dialoginterface dialog, int which) {//TODO auto-generated method stubstring text= ""; for (int i=0;i<4;i++) {text+=checkeditems[i]?items[i]+ ",": ";} Toast.maketext (mainactivity.this, text, 0). Show ();d Ialog.dismiss ();}); Builder.setnegativebutton ("Cancel", new Onclicklistener () {@Overridepublic void OnClick (dialoginterface dialog, int which) {//TODO auto-generated Method stub}}); Builder.show ();} public void Click4 (View v) {alertdialog.builder builder=new Builder (this); Builder.seticon (R.drawable.ic_launcher); Builder.settitle ("Please choose the city you Like");final string[] items=new string[]{"Beijing", "Shanghai", "Nanjing", "Shenzhen", "Nanyang"};builder.setsinglechoiceitems (items,-1, New Onclicklistener () {@Overridepublic void OnClick (dialoginterface dialog, int which) {//TODO auto-generated method Stubtoast.maketext (Mainactivity.this, " The city you selected is: "+items[which", 0). Show ();}}); Builder.show ();} public void Click5 (View v) {alertdialog.builder builder=new Builder (this); Builder.seticon (R.drawable.ic_launcher); Builder.settitle ("Please enter user name and password");
View view=layoutinflater.from (mainactivity.this). Inflate (R.layout.dialog, null); Builder.setview (view);Final EditText username= (EditText) Findviewbyid (r.id.username); final EditText password= (EditText) Findviewbyid ( R.id.password); Builder.setpositivebutton ("OK", new Onclicklistener () {@Overridepublic void OnClick (dialoginterface dialog, int which) {//TODO auto-generated method stubString A=username.gettext (). toString (). Trim (); String B=password.gettext (). toString (). Trim ();Toast.maketext (Mainactivity.this, "username:" +a+ ", Password:" +b, 0). Show ();}); Builder.setnegativebutton ("Cancel", new Onclicklistener () {@Overridepublic void OnClick (dialoginterface dialog, int which) {//TODO auto-generated Method stub}}); Builder.show ();}}

1. dialog box 1

We set up 3 action buttons, each of which binds a dialoginterface.onclicklistener () listener event, and then pops up some of our information in the Toast Toast dialog box. The which method represents the int value represented by the action button, and Which=-1 indicates that the click is the OK button, 2 means the Cancel button is clicked, and 3 means that the Ignore button is clicked.

2. Drop-down box

We set Setitems (charsequence[] items, Dialoginterface.onclicklistener Listener) method to set one of our drop-down list boxes. Note: Because the drop-down list box or the Lado box is displayed in the content, the message and drop-down list boxes are not available at the same time .

We can also bind it to a Dialoginterface.onclicklistener listener, and when an option is selected, the dialog box disappears . The which here represents the index of each option in the drop-down list, which we can easily get the user to select which option.

3. Drop-down Radio box

When the drop-down box pops up, when we select an option, the dialog box will not disappear, and we need to click the action button to make the dialog box disappear.

4. Lower Lado Box

The Setmultichoiceitems method is used when setting the Lado box

5. Customize pop-up dialog box

For custom pop-up dialogs, we need to specify a custom layout file ourselves.

First we need to write an XML layout file, then define our layout in it, we do not need to define the button in the layout file, you can set the action buttons by Alertdialog.builder.

Through view view = Layoutinflater.from (Mainactivity.this). Inflate (r.layout.dialog, NULL); We can load our layout file, get a View object, and then set up our custom pop-up box by Alertdialog.builder's Setview method.

 

Android UI Series--dialog box (i) (Alertdialog,timepickerdialog,datepickerdialog,progressdialog)

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.