Android Customization dialog box

Source: Internet
Author: User

Android Customization dialog box

2015-04-02 18:27:02

Sometimes Android built-in dialog box does not meet our needs, perhaps the function is not complete or not in line with our interface design requirements, this time we need to get a set of our own dialog box, below I will tell you a very simple way to create their own dialog box.

Create a style first: <style name= "You name Yourself" parent= "@android: Theme.dialog" >.......</style> This I believe everyone knows.

Then create your own dialog box layout: What you want to design yourself

Create your own dialog class to inherit the dialog, namely:

Public Mydialog extends dialog{

This class contains this construction method.

Public Mydialog (context context, int theme) {
Super (context, theme);
This.context = context;

}

And then create this method, which is actually similar to the activity

protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
This.setcontentview (r.layout. Dialog layout you created);

}

}

It is important to note that if you have a dialog box with the event you want to handle, such as the best implementation Onclicklistener interface for exiting a dialog or clicking a button

And then you use your own dialog in the activity.

Dialog Dialog = new Mydialog (Applicationcaractivity.this,
R.style.mydialog); Note R.style.mydialog This is the style you created.

Below I put the code of the process I have done below:

The style created first:

<name= "Mydialog"  parent= "@android: Theme.dialog">          <item name= "Android:windowframe" > @null </item>        <item name= "Android:windownotitle ">true</item>         <item name=" Android:windowbackground "> @drawable/ic_launcher</item>        <item name= "android:windowisfloating" >true</item>        <item name= "Android: Windowcontentoverlay "> @null </item>           </style>

I'm not going to explain what's inside.

And then there's my dialog layout: layout whatever it is, everybody just look at it.

<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Fill_parent"Android:layout_height= "Fill_parent"Android:background= "#ffffff"android:orientation= "vertical" >        <LinearLayoutAndroid:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"android:orientation= "Horizontal">        <DatePickerAndroid:id= "@+id/date_picker1"Android:calendarviewshown= "false"Android:layout_width= "Wrap_content"Android:layout_height= "150DP"style= "@style/apptheme"/>    <TimepickerAndroid:id= "@+id/time_picker1"Android:layout_width= "Wrap_content"Android:layout_height= "150DP"/>    </LinearLayout>    <LinearLayoutAndroid:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"android:orientation= "Horizontal"android:gravity= "Center">        <ButtonAndroid:id= "@+id/butn_sure"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "OK"/>      <ButtonAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:background= "#ffffff"Android:text="          "/>        <ButtonAndroid:id= "@+id/butn_no"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Cancel"/>    </LinearLayout></LinearLayout>

This is a custom dialog.

Class Mydialog extends Dialog implements Android.view.View.OnClickListener {private context context;        Private Button butn;        Private Timepicker Timepicker;        Private DatePicker DatePicker;        Public Mydialog (Context context) {//super (context);            //TODO auto-generated constructor stub//} public Mydialog (context context, int theme) {            Super (context, theme);            This.context = context;  TODO auto-generated constructor stub} protected void OnCreate (Bundle savedinstancestate) {//        TODO auto-generated Method Stub super.oncreate (savedinstancestate); SetTheme (Android.            R.style.theme_devicedefault_light_noactionbar_fullscreen);            This.setcontentview (r.layout.dialog_test);            DatePicker = (datePicker) Findviewbyid (r.id.date_picker1);            Timepicker = (timepicker) Findviewbyid (r.id.time_picker1); Resizepikcer (DatePicker);            Butn = (Button) Findviewbyid (r.id.butn_sure);            EDT = (EditText) Findviewbyid (R.id.number);        Butn.setonclicklistener (this);  } @Override public void OnClick (View arg0) {//TODO auto-generated method stub switch (Arg0.getid ())                {Case R.id.butn_sure://String str = Edt.gettext (). toString ();                Edt1.settext (str);                StringBuffer sb = new StringBuffer ();                        Sb.append (String.Format ("%d-%02d-%02d%02d:%02d", Datepicker.getyear (), Datepicker.getmonth () + 1,                        Datepicker.getdayofmonth (), Timepicker.getcurrenthour (),                Timepicker.getcurrentminute ()));                if (i = = 1) {edt5.settext (SB);                } else if (i = = 2) {edt6.settext (SB);                } This.dismiss (); Break; }        }    }    

Finally, in the use of activity, I write in a method:

private void Mydialog () {        Dialog Dialog = new Mydialog (applicationcaractivity.this,                r.style.mydialog);        window window = Dialog.getwindow ();//Builder.getwindow ();        Window.setgravity (Gravity.bottom); Here you can set the location of the dialog display        window.setwindowanimations (r.style.mystyle);        Dialog.settitle ("Please set the time");        Dialog.show ();                Dialog.getwindow (). setlayout (620, 414);                 /         * * Set the size of the dialog box as a percentage of the screen size         *        /WindowManager m = Getwindowmanager ();        Display d = m.getdefaultdisplay (); Get screen width, high with        windowmanager.layoutparams p = window.getattributes ();//Get dialog box current parameter value        p.height = (int) ( D.getheight () *0.4); The height is set to the screen of 0.6        p.width = (int) (D.getwidth ());//width is set to the 0.65        Window.setattributes (p) of the screen;                                    }

We refer to each other to learn, small caught dead. Thank you for coming to our humble abode!

Android Customization dialog box

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.