The Android UI defines its own dialog (when the project framework is built, it is done with less effort)

Source: Internet
Author: User

Customizing a dialog:

Before a lot of blog has been introduced in this respect, but a person think usually is not very comprehensive, universality is not very strong, generally will define a own dialog class, and then go to use, is every dialog to define a class??

First: Dialog generally contains a title section, Content section, button section, Style section. ProgressDialog is one more progress bar

Then we might as well write a dialog class, in the construction method, we put the title, content, button information to him, and then can show out

Then, in the constructor method, add an interface that uses the callback for the button that determines, cancels, and so on.

So, here we go:

The first step is to define your own dialog class

ImportAndroid.app.Dialog;ImportAndroid.content.Context;ImportAndroid.content.DialogInterface;ImportAndroid.os.Bundle;ImportAndroid.view.View;ImportAndroid.widget.Button;ImportAndroid.widget.TextView;ImportCOM.EASIPASS.R; Public  class custormdialog extends Dialog  implements  Dialoginterface {    PrivateString title;PrivateString content;PrivateDialogcallback callback;Private intIndex/***     *@paramContext *@paramTitle of the title dialog box *@paramContent dialog Box Contents *@paramTheme corresponding style here for r.style.customdialog_1 customizable style *@paramDialogcallback determine the callback for the Cancel button is oncancle onOk *@paramThe index displays several button 1 for only one OK key, the other for having OK cancel two buttons * * Call instance * Dialog = new Custormdialog (settingsactivity.this, "cache Clean up ", *" Click OK to clear the following historical information for you: \ n system notification, suitcase ticket, industry information, packing list entry ", R.style.customdialog_1, *new dialogcallback () {*@Override*public void Okdown () {* Dialog.dismiss (); *//place OK button response *} *@Override*public void Cancledown () {* Dialog.dismiss ();         *//Place Cancel button response *}},2); */         PublicCustormdialog (Context context,string title,string content,intTheme,dialogcallback Dialogcallback,intIndex) {Super(context, theme); This. title = title; This. Content = content; This. callback = Dialogcallback; This. index = index; }@Override        protected voidOnCreate (Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate);            Setcontentview (R.layout.dg_custormdialog);            TextView titl = (TextView) Findviewbyid (r.id.title);                          TextView cont = (TextView) Findviewbyid (r.id.tv_content);            Titl.settext (title);                        Cont.settext (content);            button Cancel = (Button) Findviewbyid (R.id.cancel); Button OK = (button) Findviewbyid (r.id.sure);if(Index = =1) {cancel.setvisibility (view.gone); }Else{Cancel.setonclicklistener (NewAndroid.view.View.OnClickListener () { Public voidOnClick (View v) {Custormdialog. This. dismiss (); Callback.                Cancledown ();            }            }); } ok.setonclicklistener (NewAndroid.view.View.OnClickListener () {@Override                 Public voidOnClick (View v) {Custormdialog. This. dismiss (); Callback.                Okdown ();        }            }); }                }

This class implements the interface Dialogcallback

Then define this callback:

 Public  interfacedialogcallback {    Abstract void Okdown ();    Abstract void Cancledown (); }

The preliminary framework has a

Next we write the layout (of course, the layout is completely self-played)

<?xml version= "1.0" encoding= "Utf-8"?><linearlayout  xmlns:android  =< Span class= "value" > "http://schemas.android.com/apk/res/android"  android:id  = "@+id/mian_container"  android:layout_width  = "300DP"  android:layout_height  = "Wrap_ Content " android:background  =" @color/easipass_dailog_ Bg_blue " android:orientation  =" vertical " 
    
     android:padding  =
      "10dip" ; 
            <textview  android:id  = "@+id/title"  android:layout_width  = "Fill_pare            NT " android:layout_height  =" wrap_content "            android:text  = "@string/notification_version_name"  android:textsize  = "18SP" />         <view  android:layout_width  = "fill_parent"  android:layout_height  = "1            DP " android:layout_margintop  =" 10dip " android:background  = "@drawable/cmb_list_separator_line" />         <textview  android:id  = "@+id/tv_content"  android:layout_width  = "fill            _parent " android:layout_height  =" wrap_content " android:layout_margintop  = "20dip"  android:gravity  = "center_horizontal"  an Droid:text  = "@string/version_isup"  android:textsize= "18SP" />         <linearlayout  android:layout_width= "fill_parent"  android:layout_height  =" wrap_content " android:layout_margintop  =" 10DP "            /span> android:gravity  = "Center_horizontal"  android:padding  = "10dip" ;             <button  android:id  = "@+id/sure"  android:layout_width  = "120d                P " android:layout_height  =" wrap_content " android:text  = "@string/ok"  android:textsize  = "@dimen/main_content_text_size" />             <buttonandroid:id="@+id/cancel"android:layout_width="120DP"  android:layout_height="Wrap_content"android:text="@string/cancel" android:textsize="@dimen/main_content_text_size" />                                                                                        </linearlayout></linearlayout>

The system comes with a style that usually lets us not be bird, it has a white border, and so on, then we'd better define a style of our own

<style name="Customdialog_1"Parent="@android: Style/theme.dialog"><item name="Android:windowframe">@null</Item>        <item name="android:windowisfloating">True</Item>        <item name="Android:windowistranslucent">False</Item>        <item name="Android:windownotitle">True</Item>        <item name="Android:windowbackground">@null</Item>        <item name="android:backgrounddimenabled">False</Item>    </style>

If everything goes well, let's try it.

New Custormdialog (settingsactivity.  this,"put title","Put content", r.style.customdialog_1,    new dialogcallback () {@Override  publicvoid Okdown () {                                   Dialog.dismiss ();} @Override  Public void Cancledown () {                Dialog.dismiss ();}        },2);

Here's 2 is the show ok cancel two buttons, if fill 1, then only OK button

Play It Yourself

Personal layout of the comparison is ugly, I believe that people than I have a lot of literature

With this class, you'll have the OK Cancel button later, and only the dialog box with the OK button is all done.

PS, if you want to do a one-time progressdialog, then use a frame layout, it is good, to add a parameter to the structure, nonsense not to say, I believe we are not so stupid, extrapolate

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.