Use of the "original" Android dialog box

Source: Internet
Author: User

The dialog box is Dialog ,Google 's official explanation:A Dialog is usually a small window the appears in front of th E current Activity. The underlying Activity loses focus and the dialog accepts all user interaction.

The dialog box is a small window on the current activity, and the dialog box accepts the user's action without the user's feedback.

In general, we do not instantiate Dialog directly. we're using Dialog subclasses. Below are some of the common subclasses of Dialog

For dialog boxes: Roughly three steps: Creating a dialog box, displaying a dialog box show (), Dismiss () dialog box

Alertdialog:

A dialog that can manage zero, one, B, or three buttons, and/or a list of selectable items that can include checkboxes O R radio buttons. The Alertdialog is capable of constructing most dialog user interfaces and is the suggested dialog type

ProgressDialog:

A dialog that displays a progress wheel or progress bar. Because It's an extension of the alertdialog, it also supports buttons.

Datepickerdialog:

A dialog that allows the user to select a date.

TIMEPICKERDIAOG:

A dialog that allows the user to select a time.

Of course we will also use our own custom dialog box.

The various dialog boxes are used as follows:

The code below clicks the corresponding button to create a dialog box

Package Leemo. Dialogep;import Android.app.activity;import Android.app.alertdialog;import Android.app.dialog;import Android.app.progressdialog;import Android.content.context;import Android.content.dialoginterface;import Android.os.bundle;import Android.view.view;import Android.view.view.onclicklistener;import Android.widget.Button; Import Android.widget.imageview;import android.widget.textview;import android.widget.toast;/* * Practice Operation Dialog Use */ public class Dialogepactivity extends activity {/** Called when the activity is first created. */protected void OnCreate (B Undle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.main);//SetContentView ( R.layout.main); Button Btalert = (button) Findviewbyid (R.id.btalert) btalert.settext ("Click Create Alert Dialog"); Btalert.setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick (View v) {//TODO auto-generated method Stubcreatealertdialog ();}); Button btlist = (button) Findviewbyid (r.id.btlist); Btlist.settext ("Click Create LisTdialog ") Btlist.setonclicklistener (new Onclicklistener () {@Overridepublic void OnClick (View v) {//TODO Auto-generated method Stubcreatelistdialog ();}}); Button BTCB = (button) Findviewbyid (R.ID.BTCB) btcb.settext ("Click Create Dialog with checkbox"); Btcb.setonclicklistener (new Onclicklistener () {@Overridepublic void OnClick (View v) {//TODO auto-generated method Stubcreatesinglecheckboxdialog () ;}}); Button BTPB = (button) Findviewbyid (R.ID.BTPB) btpb.settext ("Click Create progress Bar"); Btpb.setonclicklistener (New Onclicklistener ( {@Overridepublic void OnClick (View v) {//TODO auto-generated method Stubcreatepbdialog ();}}); Button Btcusdialog = (button) Findviewbyid (R.id.btcusdialog); Btcusdialog.settext ("Click Create custom dialog box"); Btcusdialog.setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick (View v) {//TODO auto-generated Method Stubcreatecusdialog ();}});} public void Createalertdialog () {Alertdialog.builder Builder = new Alertdialog.builder (this); Builder.setmessage ("is You sure want to exit "). Setcancelable (FALSE). Setpositivebutton ("Yes", new Dialoginterface.onclicklistener () {@Overridepublic void OnClick (dialoginterface Dialog,int which) {//TODO auto-generated method StubDialogEpActivity.this.finish ();}}). Setnegativebutton ("No", new Dialoginterface.onclicklistener () {@Overridepublic void OnClick (Dialoginterface dialog, int which) {//TODO auto-generated method Stubdialog.cancel ();}}); Builder.create (). Show (); public void Createlistdialog () {final charsequence[] items = {"Red", "Blue", "Green"}; Alertdialog.builder Builder = new Alertdialog.builder (dialogepactivity.this); Builder.settitle ("Select A Color"); Builder.setitems (items, new Dialoginterface.onclicklistener () {@Overridepublic void OnClick (Dialoginterface dialog, int which) {//TODO auto-generated method Stubtoast.maketext (Getapplication (), Items[which],toast.length_long). Show () ;}}); Builder.create (). Show (); public void Createsinglecheckboxdialog () {final charsequence[] items = {"Red", "Bule", "Green"}; Alertdialog.builder bd = new AlErtdialog.builder (Dialogepactivity.this); Bd.settitle ("Select A Color"); Bd.setsinglechoiceitems (items, -1,new Dialoginterface.onclicklistener () {@Overridepublic void OnClick (dialoginterface dialog, int which) {//TODO Auto-generated method Stubdialog.dismiss (); Toast.maketext (Getapplicationcontext (), Items[which],toast.length_short). Show ();}}); Bd.create (). Show (); public void Createpbdialog () {progressdialog dialog = Progressdialog.show (dialogepactivity.this, "Loading", "Loading Please waiting ");d ialog.show ();} public void Createcusdialog () {Context Mcontext = dialogepactivity.this;dialog Dialog = new Dialog (mcontext); Dialog.setcontentview (R.layout.dialog);d ialog.settitle ("Custom Dialog"); TextView Text = (TextView) Dialog.findviewbyid (R.id.text); Text.settext ("Hello, this is a custom dialog!"); Mageview image = (ImageView) Dialog.findviewbyid (r.id.image); Image.setimageresource (R.drawable.ic_launcher); Button Btcs = (button) Dialog.findviewbyid (R.id.btcs) btcs.settext ("Click Create Toast"); Btcs.setonclIcklistener (New Onclicklistener () {@Overridepublic void OnClick (View v) {//TODO auto-generated method Stubtoast.maketext (Dialogepactivity.this, "is okay", Toast.length_short). Show ();}); Dialog.show ();}}

To define a layout file yourself when creating a custom dialog box

<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android"              android:id= "@+id/layout_root"              android:orientation= "Horizontal"              android:layout_width= "fill_parent"              android:layout_height= "Fill_ Parent "              android:padding=" 10DP "              >    <imageview android:id=" @+id/image "               android:layout_width= "Wrap_content"               android:layout_height= "fill_parent"               android:layout_marginright= "10DP"               />    <textview android:id= "@+id/text"              android:layout_width= "wrap_content" android:layout_height= "Fill_              Parent "              android:textcolor=" #FFF "              />    <button         android:id=" @+id/btcs "        android: Layout_width= "Wrap_content"        android:layout_height= "wrap_content"        /></linearlayout>

Effects such as

Use of the "original" Android dialog box

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.