Android Apidemos Learning--time-date control

Source: Internet
Author: User

Time and date process will be used in normal development, Android built-in two interactive time-date control, one is the form of a dialog box, one is embedded.

Android classes Timepickerdialog, Datepickerdialog, and Timepicker are used respectively, where timepicker needs to be set in layout


A time control in the form of a dialog box

public class Datedialog extends Activity {//Where we display the selected date and time private TextView Mdatedisp    Lay    Date and time private int myear;    private int mmonth;    private int mday;    private int mhour;    private int mminute;    static final int time_dialog_id = 0;    static final int date_dialog_id = 1;        @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (r.layout.date_widgets_example_1);        Mdatedisplay = (TextView) Findviewbyid (R.id.datedisplay);        Button pickdate = (button) Findviewbyid (r.id.pickdate); Pickdate.setonclicklistener (New View.onclicklistener () {public void OnClick (View v) {Showdialo            g (date_dialog_id);        }        });        Button Picktime = (button) Findviewbyid (r.id.picktime); Picktime.setonclicklistener (New View.onclicklistener () {public void OnClick (View v) {Showdialo G(time_dialog_id);        }        });        Final Calendar C = calendar.getinstance ();        Myear = C.get (calendar.year);        Mmonth = C.get (calendar.month);        Mday = C.get (calendar.day_of_month);        Mhour = C.get (Calendar.hour_of_day);        Mminute = C.get (Calendar.minute);    Updatedisplay ();                } @Override protected Dialog oncreatedialog (int id) {switch (ID) {Case time_dialog_id:            return new Timepickerdialog (this, Mtimesetlistener, Mhour, Mminute, false);                            Case Date_dialog_id:return New Datepickerdialog (This, Mdatesetlistener,        Myear, Mmonth, Mday);    } return null; } @Override protected void onpreparedialog (int id, Dialog Dialog) {switch (ID) {Case time_dialog                _ID: ((Timepickerdialog) dialog). UpdateTime (Mhour, Mminute);            Break Case Date_dIALOG_ID: ((Datepickerdialog) dialog). Updatedate (Myear, Mmonth, Mday);        Break                    }} private void Updatedisplay () {Mdatedisplay.settext (New StringBuilder () Month is 0 based so add 1. Append (Mmonth + 1). Append ("-"). Append (Mday). Append ( "-"). Append (Myear). Append (""). Append (Pad (mhour)). Append (":").    Append (Pad (mminute)));                } private Datepickerdialog.ondatesetlistener Mdatesetlistener = new Datepickerdialog.ondatesetlistener () {                    public void Ondateset (DatePicker view, int year, int monthofyear, int dayofmonth) {                    Myear = year;                    Mmonth = Monthofyear;                    Mday = DayOfMonth;                Updatedisplay ();    }            };       Private Timepickerdialog.ontimesetlistener Mtimesetlistener =     New Timepickerdialog.ontimesetlistener () {public void Ontimeset (timepicker view, int hourofday, int m                    Inute) {mhour = Hourofday;                    Mminute = minute;                Updatedisplay ();    }            };        private static String pad (int c) {if (c >=) return string.valueof (c);    else return "0" + string.valueof (c); }}

Inline-Time Controls

public class Dateincline extends Activity {//Where we display the selected date and time private TextView Mtimedis    Play        @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (r.layout.date_widgets_example_2);        Timepicker Timepicker = (timepicker) Findviewbyid (R.id.timepicker);        Timepicker.setcurrenthour (12);        Timepicker.setcurrentminute (15);        Mtimedisplay = (TextView) Findviewbyid (R.id.datedisplay);        Updatedisplay (12, 15); Timepicker.setontimechangedlistener (New Timepicker.ontimechangedlistener () {public void ontimechanged (TimePick            ER view, int hourofday, int minute) {Updatedisplay (hourofday, minute);    }        }); } private void Updatedisplay (int hourofday, int minute) {Mtimedisplay.settext (new Stringbui Lder (). Append (Pad (hourofday)). Append (":"). Append (PAD (minute)));        } private static String pad (int c) {if (c >=) return string.valueof (c);    else return "0" + string.valueof (c); }}


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.