Features and usage of date, time picker (DatePicker and Timepicker)
DatePicker and Timepicker are two more common controls that derive from Framelayout , where DatePicker is used to select dates,timepicker used to select the time.
DatePicker and Timepicker provide some methods to get the date and time selected by the current user on the basis of framelayout, and developers can add them to the DatePicker by adding Ondatechangedlistener, adding ontimechangedlistener for timepicker The listener listens to get the date and time the user chooses.
DatePicker (date selector) structure:
<pre name= "code" class= "HTML" >java.lang.object ? android.view.View? android.view.ViewGroup? Android.widget.FrameLayout ? android.widget.DatePicker
Overview:
a calendar layout view that selects month and day, and for the dialog view, see datepickerdialog.
Public methods:
|
Public Methods |
|
Int |
GetDayOfMonth ()
Gets the selected number of days |
|
Int |
GetMonth ()
Get the selected month |
|
Int |
GetYear ()
Gets the selected year |
|
void |
Init (int year, int monthofyear, int dayofmonth, datepicker.ondatechangedlistener Ondatechangedlistener)
Date of initial date |
|
void |
setenabled (Boolean enabled)
To set the enabled state of a view |
|
void |
updatedate (int year, int monthofyear, int dayofmonth)
Update Date |
Datepickersupported XMLProperty:
|
XML Properties |
Description |
|
Android:calendarviewshown |
sets whether the date selector is displayed Clendarview Components |
|
Android:endyear |
Set this date selector to allow selection of the last year |
|
Android:startyear |
Set this date selector to allow the 11th year of selection |
|
Android:maxdate |
sets the maximum date that the date selector allows for selection. Specify the maximum date in mm/dd/yyyy format |
|
Android:mindate |
sets the minimum date that the date selector allows for selection. Specify the Minimum date in mm/dd/yyyy format |
|
Android:spinnersshown |
sets whether the date selector is displayed Spinner Date Selection Component |
Timepicker (Time picker) structure:
<pre name= "code" class= "HTML" >ava.lang.object ? android.view.View? android.view.ViewGroup? Android.widget.FrameLayout ? android.widget.TimePicker
Overview:
Used to select a view of the time of day, 24-hour and morning/afternoon modes are supported. Hours, minutes, and morning/afternoon (if available) can be controlled with a vertical scroll bar. Use the keyboard to enter the hour. The two-digit hours can be achieved by entering two digits, such as entering ' 1 ' and ' 2 ' in a certain amount of time to select 12 points. Minutes can display a single number entered. In am/pm mode, the user can enter ' a ', ' a ' or ' P ', ' P ' to select. For the dialog view, see timepickerdialog.
Public methods:
|
Public Methods |
|
|
Int |
Getbaseline ()
Returns the offset of the text baseline of the window space to its top boundary. If this part does not support baseline alignment,
This method returns -1/. |
|
Integer |
Getcurrenthour ()
Gets the hour portion of the current time. return value current hour (0-23) |
|
Integer |
Getcurrentminute ()
Gets the minute part of the current time |
|
Boolean |
Is24hourview ()
Gets whether the current system setting is 24-hour. Returns true if it is a 24-hour system, otherwise false is returned. |
|
void |
Setcurrenthour (Integer currenthour)
Set Current Hour |
|
void |
Setcurrentminute (Integer currentminute)
Set Current minute (0-59) |
|
void |
setenabled (Boolean enabled)
Sets the available view state. The interpretation of the available view state is changed in the subclass. |
|
void |
Setis24hourview (Boolean is24hourview)
setting is 24 hours or am/PM system |
|
void |
Setontimechangedlistener (timepicker.ontimechangedlistener ontimechangedlistener)
Sets the callback function for the time adjustment event. |
Application Examples Select Date and time: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: * <br/> Select Date Time * <br/> This instance through DatePicker (Time picker) and * <br/> Timepicker (date selector) to provide the user with the choice of date and time * <br/> in order to get the selected date program for DatePicker registered Ondatechangedlistener * <br/> Listener, In order to get the selected time program for Timepicker registration Ontimechangedlistener * <br/> Listener * @author JPH * date:2014.20.57 */public class Choosedate Extends activity{//defines 5 variables for the current time private int year;private int month;private int day;private int hour;private int Minute;@o verridepublic void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.main);D Atepicker DatePicker = (DatePicker) Findviewbyid (R.id.datepicker); Timepicker Timepicker = (timepicker) Findviewbyid (r.id.timepicker);//Gets the current year, month, day, hour, minute Calendar C = calendar.getinstance (); years = C.get (calendar.year); month = C.get (calendar.month);d ay = c.get (calendar.day_of_month); hour = C.get (calendar.hour); minute = C.get ( Calendar.minute);//Initialize DatePicker component, specify listener Datepicker.init when initializing (year, month, day, new Ondatechangedlistener () {@ overridepublic void ondatechanged (DatePicker arg0, int year, int month, int.) {ChooseDate.this.year = year; ChooseDate.this.month = month; ChooseDate.this.day = day;//Displays 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 Listener Timepicker.setontimechangedlistener for Timepicker (new Ontimechangedlistener () {@Overridepublic void Ontimechanged (timepicker view, int hourofday, int minute) {ChooseDate.this.hour = Hourofday; ChooseDate.this.minute = minute;//Displays the current date, time Showdate (year, month, day, hour, minute); Toast.maketext (Choosedate.this, "The time you selected:" +hourofday+ "when" +minute+ "points", Toast.length_short). Show (),//}}); Defines the method that displays the current date and time in EditText private void showdate (int year, int month, int day, int hour, int minute) {EditText show = (Edit Text) Findviewbyid (r.id.show); Show.settext ("The Date and time you selected are:" + year + "years" + (month + 1) + "Month" + Day + "days" + Hour + "when" + Minu Te + "min");}}
XML layout 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 " ><!--Display user input date, time control--><edittext android:id= "@+id/show" android:layout_width= "Fill_parent" Android: layout_height= "Wrap_content" android:editable= "false" android:cursorvisible= "false"/><textview Android: Layout_width= "Fill_parent" android:layout_height= "wrap_content" android:text= "Please select date and time"/><!-- Define a DatePicker component--><datepicker android:id= "@+id/datepicker" android:layout_width= "Wrap_content" Android: layout_height= "200DP" android:layout_gravity= "center_horizontal" android:startyear= "2050" android:endyear= " Android:calendarviewshown= "true" android:spinnersshown= "true"/><!--define a Timepicker component--><timepicker Android:id= "@+id/timepicker" android:layout_width= "wrap_content" android:layout_height= "100DP" Android:layout_ Gravity= "Center_horizontaL "/></linearlayout>