The DatePicker control inherits from the Framelayout class, and the primary function of the date selection control is to provide the user with date data that contains the year, month, and day and allow the user to modify it.
The Timepicker control inherits from the Framelayout class, showing the user the time of day, allowing the user to modify it.
Methods for DatePicker controls:
1.init ()
Initializes the DatePicker date selection control, sets the initial display of the year, month, number of days, and sets the date change listener. One of the monthofyear parameters is the initial month, and the actual month displayed is the value set here plus 1.
2.OnDateChangedListener ()
Set the date listener.
3.updateDate
Update the DatePicker control, and calling the method will trigger a date change listener.
Methods for Timepicker Controls:
1.setCurrentHour ()
Sets the hour for timepicker display.
2.setCurrentMinute ()
Sets the timepicker displayed in minutes.
3.setis24hourview ()
The time selection control is displayed in 24-hour binary.
4.setOnTimeChangedListener ()
Set the time change listener.
Small instance:
Mainactivity.java
public class Mainactivity extends Activity {DatePicker DatePicker; Timepicker Timepicker; TextView timestr, datestr; @Overridepublic void OnCreate (Bundle savedinstancestate) {super.oncreate ( Savedinstancestate); Setcontentview (r.layout.activity_main);d Atepicker = (datePicker) Findviewbyid (R.id.date); Timepicker = (timepicker) Findviewbyid (r.id.time); timestr = (TextView) Findviewbyid (r.id.time_str);d atestr = (TextView ) Findviewbyid (R.ID.DATE_STR); Calendar calendar = calendar.getinstance (); int year = Calendar.get (calendar.year); int monthofyear = Calendar.get ( calendar.month); int dayofmonth = Calendar.get (calendar.day_of_month);d atepicker.init (year, Monthofyear, DayOfMonth, New Ondatechangedlistener () {@Overridepublic void ondatechanged (DatePicker arg0, int year, int monthofyear,int DayOfMonth) {Datestr.settext (year + "years" + (monthofyear+1) + "month" +dayofmonth+ "Day");}); Timepicker.setontimechangedlistener (New Ontimechangedlistener () {@Overridepublic void ontimechanged (Timepicker arg0 , int hour, int minutER) {timestr.settext (hour+ "when" +minuter+ ");}});}} Activity_main.xml
<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools "android:layout_width=" match_parent "android:layout_height=" Match_parent "Android:orien tation= "vertical" tools:context= "${relativepackage}.${activityclass}" > <linearlayout android:layout_wi Dth= "Fill_parent" android:layout_height= "0DP" android:orientation= "Horizontal" android:layout_weight= "1" > <textview android:id= "@+id/date_str" android:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:text= "/> <textview android:id=" @+id /time_str "android:layout_width=" wrap_content "android:layout_height=" Wrap_content "Andro Id:text= ""/> </LinearLayout> <datepicker android:id= "@+id/date" android:layout_width= "Wra P_content "android:layout_height=" 0DP " android:layout_weight= "3"/> <timepicker android:id= "@+id/time" android:layout_width= "Wrap_co Ntent "android:layout_height=" 0DP "android:layout_weight=" 3 "/></linearlayout>
:
Android-developed DatePicker controls and Timepicker controls