My android advanced journey --> Android datepicker and timepicker Cases

Source: Internet
Author: User

Introduction

The following uses an example to show the functions and usage of datepicker and timepicker.

Step 1: Create a project dateandtimepicker

Step 2: Compile the app UI/layout/Main. xml

<? 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: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: text = "select the time to purchase this 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: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: editable = "false" Android: cursorvisible = "false" Android: id = "@ + ID/Show"/> </linearlayout>

Step 3: main program mainactivity. Java

Package CN. roco. picker; import Java. util. calendar; import android. app. activity; import android. OS. bundle; import android. widget. datepicker; import android. widget. datepicker. ondatechangedlistener; import android. widget. edittext; import android. widget. timepicker; import android. widget. timepicker. ontimechangedlistener; public class mainactivity extends activity {private int year; private int month; private int day; private int hour; private int minute;/** called when the activity is first created. * // @ overridepublic void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); datepicker = (datepicker) findviewbyid (R. id. datepicker); timepicker = (timepicker) findviewbyid (R. id. timepicker); Calendar calendar = calendar. getinstance (); year = calendar. get (calendar. year); month = calendar. get (calendar. month); Day = calendar. get (calendar. day_of_month); hour = calendar. get (calendar. hour); minute = calendar. get (calendar. minute); datepicker. init (year, month, day, new ondatechangedlistener () {@ overridepublic void ondatechanged (datepicker view, int year, int month, int day) {mainactivity. this. year = year; mainactivity. this. month = month; mainactivity. this. day = day; // display time showdate (year, month, day, hour, minute) ;}}); timepicker. setontimechangedlistener (New ontimechangedlistener () {@ overridepublic void ontimechanged (timepicker view, int hour, int minute) {mainactivity. this. hour = hour; mainactivity. this. minute = minute; // display time showdate (year, month, day, hour, minute) ;}}) ;}private void showdate (INT year, int month, int day, int hour, int minute) {edittext show = (edittext) findviewbyid (R. id. show); show. settext ("your purchase date is: "+ year +" year "+ month +" month "+ day +" day "+ hour +" Hour "+ minute +" Minute ");}}

Step 4: deploy the application to the simulator and select the date and time

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.