Functions and usage of Date and Time selector (DatePicker and TimePicker) in Android Development

Source: Internet
Author: User

Functions and usage of Date and Time selector (DatePicker and TimePicker) in Android Development

Functions and usage of Date and Time Picker (DatePicker and TimePicker)

DatePicker and TimePicker are two common controls derived from FrameLayout. DatePicker is used to select a date and TimePicker is used to select a time.

DatePicker and TimePicker provide methods based on FrameLayout to obtain the date and time selected by the current user; developers can listen to DatePicker by adding OnDateChangedListener and OnTimeChangedListener for TimePicker to obtain the date and time selected by the user.

DatePicker (date selector)Structure:
java.lang.Object   ? android.view.View     ? android.view.ViewGroup       ? android.widget.FrameLayout         ? android.widget.DatePicker
Overview:

A calendar Layout View that selects year, month, and day. For the dialog box view, see DatePickerDialog.

Public method:

Public Methods

Int

GetDayOfMonth ()

Obtain the number of days selected

Int

GetMonth ()

Obtain the selected month

Int

GetYear ()

Obtain the selected year

Void

Init (int year, int monthOfYear, int dayOfMonth, DatePicker. OnDateChangedListener onDateChangedListener)

Initialize year, month, and day

Void

SetEnabled (boolean enabled)

Enable View

Void

UpdateDate (int year, int monthOfYear, int dayOfMonth)

Update date

DXML attributes supported by atePicker:

XML attributes

Description

Android: calendarViewShown

Sets whether the ClendarView component is displayed for the date selector.

Android: endYear

Set the last year allowed by the date Selector

Android: startYear

Set the Year 11 date that can be selected by the date Selector

Android: maxDate

Set the maximum date allowed by the date selector. Specify the maximum date in mm/dd/yyyy format

Android: minDate

Set the minimum date allowed by the date selector. Specify the minimum date in mm/dd/yyyy format

Android: spinnersShown

Sets whether the date selector displays the Spinner date Selection Component.

TimePicker (Time selector)Structure:
ava.lang.Object   ? android.view.View     ? android.view.ViewGroup       ? android.widget.FrameLayout         ? android.widget.TimePicker
Overview:

This is used to select a view of the time of a day. The 24-hour and morning/afternoon modes are supported. You can use a vertical scroll bar to control the hour, minute, and morning/afternoon (if available. Enter the hour on the keyboard. You can enter two numbers for two hours. For example, if you enter '1' and '2' within a certain period of time, 12 points is selected. A single number can be displayed in minutes. In AM/PM mode, you can enter 'A', 'a ", 'P', or 'P' to select. For the dialog box view, see TimePickerDialog.

Public method:

Public Methods

Int

GetBaseline ()

Returns the offset from the text baseline of a window to its top boundary. If this part does not support baseline alignment,

This method returns-1 /.

Integer

GetCurrentHour ()

Obtain the hour of the current time. Return value current hour (0-23)

Integer

GetCurrentMinute ()

Obtain the minute of the current time

Boolean

Is24HourView ()

Checks whether the current system settings are in the 24-hour format. True is returned in the 24-hour format. Otherwise, false is returned.

Void

SetCurrentHour (Integer currentHour)

Set current hour

Void

SetCurrentMinute (Integer currentMinute)

Set the current minute (0-59)

Void

SetEnabled (boolean enabled)

Set the available view status. The description of available view states is changed in the subclass.

Void

SetIs24HourView (Boolean is24HourView)

Set to 24 hours or morning/afternoon

Void

SetOnTimeChangedListener (TimePicker. OnTimeChangedListener onTimeChangedListener)

Set the time adjustment Event Callback Function.

Select the date and time for the application instance: Run: program code:
Package com. jph. choosedae; 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; import android. widget. toast;/*** Description :*
Select date and time *
This instance uses DatePicker (Time selector) and *
TimePicker (date selector) to provide users with a date and time *
To obtain the selected date, the program registers OnDateChangedListener for DatePicker *
Listener. To obtain the selected time program, register OnTimeChangedListener for TimePicker *
Listener * @ author jph * Date: 2014.20.57 */public class ChooseDate extends Activity {// defines five variables for recording the current time: private int year; private int month; private int day; private int hour; private int minute; @ Overridepublic 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); // obtain the current year, month, day, hour, and minute Calendar c = Calendar. getInstance (); year = c. get (Calendar. YEAR); month = c. get (Calendar. MONTH); day = c. get (Calendar. DAY_OF_MONTH); hour = c. get (Calendar. HOUR); minute = c. get (Calendar. MINUTE); // initialize the DatePicker component and specify the listener datePicker during initialization. init (year, month, day, new OnDateChangedListener () {@ Overridepublic void onDateChanged (DatePicker arg0, int year, int month, int day) {ChooseDate. this. year = year; ChooseDate. this. month = month; ChooseDate. this. day = day; // display the current date, time showDate (year, month, day, hour, minute); Toast. makeText (ChooseDate. this, "the date you selected:" + year + "year" + month + "month" + day + "day", Toast. LENGTH_SHORT ). show () ;}}); // specify the listener TimePicker for timePicker. setOnTimeChangedListener (new OnTimeChangedListener () {@ Overridepublic void onTimeChanged (TimePicker view, int hourOfDay, int minute) {ChooseDate. this. hour = hourOfDay; ChooseDate. this. minute = minute; // display the current date, time showDate (year, month, day, hour, minute); Toast. makeText (ChooseDate. this, "the time you selected:" + hourOfDay + "Hour" + minute + "minute", Toast. LENGTH_SHORT ). show (); //});} // defines the private void showDate (int year, int month, int day, int hour, int minute) {EditText show = (EditText) findViewById (R. id. show); show. setText ("the date and time you selected are:" + year + "year" + (month + 1) + "month" + day + "day" + hour + "hour" + minute + "minute ");}}

Xml layout file:
   
   
    
    
    
    
    
    
    
   



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.