Android DatePicker and TimePicker Cases

Source: Internet
Author: User

The following describes the functions and usage of DatePicker and TimePicker in an example that allows users to select a date and time. Step 1: Create a project DateAndTimePicker Step 2: Compile the UI interface/layout/main of the application. xml [html] <? 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 [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. * // @ Override public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); DatePicker datePicker = (DatePicker) findViewById (R. id. datePicker); TimePicker 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 () {@ Override public 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 () {@ Override public 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.