DatePicker and Timepicker Brief introduction
DatePicker and Timepicker are inherited from Framelayout. They are all relatively simple components. Time change Time is added Ondatechangelistener () and Ontimechangelistener () to monitor.
DatePicker and Timepicker Properties
Select an instance of the date and time
Package Peng. Liu. Test;Import Android. App. Activity;Import Android. Graphics. Color;Import Android. Graphics. drawable. Drawable;Import Android. OS. Bundle;Import Android. View. Gravity;Import Android. View. Layoutinflater;Import Android. View. View;Import Android. View. ViewGroup;Import Android. Widgets. Adapterview;Import Android. Widgets. Baseadapter;Import Android. Widgets. Button;Import Android. Widgets. CalendarView;Import Android. Widgets. DatePicker;Import Android. Widgets. EditText;Import Android. Widgets. Framelayout;Import Android. Widgets. GridView;Import Android. Widgets. Imageswitcher;Import Android. Widgets. ImageView;Import Android. Widgets. LinearLayout;Import Android. Widgets. Simpleadapter;Import Android. Widgets. Textswitcher;Import Android. Widgets. TextView;Import Android. Widgets. Timepicker;Import Android. Widgets. Toast;Import Android. Widgets. Viewflipper;Import Android. Widgets. Viewswitcher;Import Java. Util. ArrayList;Import Java. Util. Calendar;Import Java. Util. HashMap;Import Java. Util. List;Import Java. Util. Map;public class Mainactivity extends Activity {private DatePicker DatePicker;Timepicker Timepicker;EditText Edit;private int Year,month,day,hour,minute;@Override protected void OnCreate (Bundle savedinstancestate) {Super. OnCreate(savedinstancestate);Setcontentview (R. Layout. Activity_main);DatePicker = (datePicker) Findviewbyid (R. ID. DatePicker);Timepicker = (timepicker) Findviewbyid (R. ID. Timepicker);Edit = (EditText) Findviewbyid (R. ID. Show);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);DatePicker. Init(Year,month,day,new DatePicker. Ondatechangedlistener() {@Override public void ondatechanged (DatePicker DatePicker, int i, int i2, int i3) { Year = i;month = I2;Day = i3;Showdate (Year,month,day,hour,minute);} });Timepicker. Setontimechangedlistener(New Timepicker. Ontimechangedlistener() {@Override public void ontimechanged (timepicker timepicker, int i, int i2) {Hour = I;minute = I2;Showdate (Year,month,day,hour,minute);} });} private void Showdate (int year,int month,int day,int hour,int minute) {Edit. SetText(year+month+day+hour+day+"");}}
<?xml version= "1.0" encoding= "Utf-8"?><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:paddingleft="@dimen/activity_horizontal_margin" Android:paddingright="@dimen/activity_horizontal_margin" Android:paddingtop="@dimen/activity_vertical_margin" Android:paddingbottom="@dimen/activity_vertical_margin" Tools:context=". Mainactivity " android:orientation="Vertical"> <TextViewandroid:layout_width="Fill_parent"android:layout_height ="Wrap_content"android:text="Select detailed Time"/> <datepicker android: Layout_width = "wrap_content" android:layout_ Height = "200DP" android:id = "@+id/datepicker" android:startyear =" " android:endyear =" 2015 "
android:layout_gravity = "Center_horizontal"
android:spinnersshown = "true" android:calendarviewshown = "true" /> <timepicker android: Layout_width = "wrap_content" android:layout_ Height = "100DP" android:id = "@+id/timepicker" android:layout_gravity =" center_horizontal "/> <EditTextandroid:layout_width="Fill_parent"android:layout_height= "Wrap_content" android:id="@+id/show"android:editable="false"android:cursorvisible ="false"/> </linearlayout>
Android DatePicker and Timepicker-android Learning Tour (38)