Use of datepicker and timepicker in Android

Source: Internet
Author: User

Let's take a look:

Post the followingCodeHere, the main. XML code is:

<? XML version = "1.0" encoding = "UTF-8"?> <Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent" Android: Orientation = "vertical"> <textview Android: id = "@ + ID/textview1" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: TEXT = "select the specific time to purchase the book"/> <datepicker Android: Id = "@ + ID/datepicker" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: layout_gravity = "center_horizontal"/> <timepicker Android: Id = "@ + ID/timepicker" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: layout_gravity = "center_horizontal"/> <edittext Android: Id = "@ + ID/Show" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: cursorvisible = "false" Android: editable = "false"/> </linearlayout>

The Java code is:

Public class androiddemo5activity extends activity {// record the current time private int year; private int month; private int day; private int hour; private int minute; @ overrideprotected void oncreate (bundle savedinstancestate) {// todo auto-generated method stubsuper. oncreate (savedinstancestate); setcontentview (R. layout. main); datepicker date = (datepicker) findviewbyid (R. id. datepicker); timepicker time = (timepicker) findviewbyid (R. id. timepicker); // The current year, month, day, hour, and minute calendar CA = calendar. getinstance (); year = Ca. get (calendar. year); month = Ca. get (calendar. month); Day = Ca. get (calendar. day_of_month); hour = Ca. get (calendar. hour); minute = Ca. get (calendar. minute); // initialize datepickerdate. init (year, month, day, new ondatechangedlistener () {@ overridepublic void ondatechanged (datepicker arg0, int year, int month, int day) {androiddemo5activity. this. year = year; androiddemo5activity. this. month = month; androiddemo5activity. this. day = day; // display the current time and date showdate (year, month, day, hour, minute) ;}}); // specify the event listener time for timerpicker. setontimechangedlistener (New ontimechangedlistener () {@ overridepublic void ontimechanged (timepicker arg0, int hour, int minute) {androiddemo5activity. this. hour = hour; androiddemo5activity. this. minute = minute ;}}) ;}protected void showdate (INT year2, int mon2, int day2, int hour2, int minute2) {edittext text = (edittext) findviewbyid (R. id. show); text. settext ("your purchase time is: "+ year2 +" year "+ mon2+" month "+ day2 +" day "+ hour2 +" Hour "+ minute2 +" Minute ");}}
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.