Android UI Control Series: Datepicker,timepicker (date and time selection)

Source: Internet
Author: User
The date and time are the features of any mobile platform, and so does Android.

DatePicker: Used to implement the date (Year month day)

Timepicker: Used to implement time (hours, minutes, seconds)
Calendar: An abstract base class that converts between an annual date object and an integer number field, such as month, day, hour, and so on.

For example

Final Calendar calendar=calendar.getinstance (); Myear=calendar.get (calendar.year);-Get year mmonth=calendar.get ( Calendar.month);-Get the Month mday=calendar.get (calendar.day_of_month);-Get the Day Mhour=calendar.get (calendar.hour_of_day);- Get Mminute=calendar.get (Calendar.minute);-Get points

And so on, you can refer to the API documentation

Timepickerdialog, Datepickerdialog is the time class in the form of dialog boxes.

Needless to say, the example is as follows:

Datetest.java file

Package Org.loulijun.datetest;import Java.util.calendar;import Android.app.activity;import Android.app.datepickerdialog;import Android.app.timepickerdialog;import Android.os.bundle;import Android.view.view;import Android.widget.button;import Android.widget.datepicker;import Android.widget.TextView; Import Android.widget.timepicker;public class Datetest extends activity {/** Called when the activity is first created .        */TextView TextView;        Timepicker Timepicker;        DatePicker DatePicker;        Button btn1;        Button btn2;    Calendar class in Java calendar C;        @Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.main); /*like other locale-sensitive classes, Calendar provides a class method, getinstance, *for getting a default Instan        Ce of this class for general use. *calendar ' s getinstance method returns a Calendar whose locale is based on system SettiNGS *and whose time fields has been initialized with the current date and time: */C=calendar.getins        Tance ();        textview= (TextView) Findviewbyid (R.id.textview);        btn1= (Button) Findviewbyid (R.id.button1);        Btn2= (Button) Findviewbyid (R.id.button2);        Gets the DatePicker object datepicker= (DatePicker) Findviewbyid (r.id.datepicker1); Initializes the calendar to the current system time and sets its event listener Datepicker.init (C.get (calendar.year), C.get (Calendar.month), C.get (calendar.day_of_month                                        ), new Datepicker.ondatechangedlistener () {@Override                                                        public void ondatechanged (DatePicker view, int year, int monthofyear, int dayofmonth) {//TODO auto-generate                                                D stub//when the current date changes, set here C.set (Year,monthoFyear,dayofmonth);        }                                });        Gets the Timepicker object timepicker= (timepicker) Findviewbyid (r.id.timepicker1);        Set to 24-hour display time Timepicker.setis24hourview (true); Listen time change Timepicker.setontimechangedlistener (new Timepicker.ontimechangedlistener () {@Overr                                IDE public void ontimechanged (timepicker view, int hourofday, int minute) { TODO auto-generated Method Stub//time Change processing//c.set (                        Year,month,hourofday,minute,second);        }                }); Btn1.setonclicklistener (New Button.onclicklistener () {@Override Publ                                IC void OnClick (View v) {//TODO auto-generated method stub               New Datepickerdialog (Datetest.this,                                 New Datepickerdialog.ondatesetlistener () {  @Override public void Ondateset (DatePicker view, int year,                                                                        int monthofyear, int dayofmonth) {                                                Set Calendar} },c.get (Calendar.year), C.get (Calendar.month), C.get (Calendar.day_of_month)). Sh                        ow ();        }        }); Btn2.setonclicklistener (New Button.onclicklistener () {@Override Publ                                IC void OnClick (View v) {//TODO auto-generated method stub New Timepickerdialog (Datetest.this, New TimepickeRdialog.ontimesetlistener () {@Override                                                                public void Ontimeset (timepicker view, int hourofday, int minute) {                                                                TODO auto-generated Method Stub                                                Set Time}                        },c.get (Calendar.hour_of_day), C.get (Calendar.minute), true). Show ();    }        }); }}

Main.xml file

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/    Android "android:orientation=" vertical "android:layout_width=" fill_parent "android:layout_height=" Fill_parent " ><textview android:id= "@+id/textview" android:layout_width= "fill_parent" android:layout_height= "Wrap_c Ontent "android:text=" Time Control using "/><datepicker android:id=" @+id/datepicker1 "android:layout_width=" Wrap_content "android:layout_height=" wrap_content "/><timepicker android:id=" @+id/timepicker1 "a Ndroid:layout_width= "Wrap_content" android:layout_height= "wrap_content"/><button android:id= "@+id/butt        On1 "android:layout_width=" wrap_content "android:layout_height=" Wrap_content "android:text=" Set Date " android:layout_gravity= "center"/><button android:id= "@+id/button2" android:layout_width= "Wrap_cont Ent "android:layout_height="Wrap_content "android:text=" Set Time "android:layout_gravity=" center "/></linearlayout> 

The results of the operation are as follows:

When you click on the "Set Date" button, the following dialog box style is displayed Datepickerdialog

When you click on the "Set Time" button, the following dialog box style will pop up Timepickerdialog

These are the Android UI Control series: Datepicker,timepicker (date and time selection), more about topic.alibabacloud.com (www.php.cn)!

  • 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.