Android --- 29 --- DatePicker, TimePicker, and androidtimepicker
DatePicker and TimePicker are controls used to display time.
OnDateChanged and onTimeChanged are the control trigger events.
MainActivity. java:
Import java. text. simpleDateFormat; import java. util. calendar; import java. util. timerTask; import android. app. activity; import android. OS. bundle; import android. view. menu; import android. view. menuItem; import android. widget. datePicker; import android. widget. textView; import android. widget. datePicker. onDateChangedListener; import android. widget. timePicker; import android. widget. timePicker. onTimeChangedListener; import android. widget. toast;/*** use of the time control ** @ author Caesar **/public class MainActivity extends Activity implements listener, OnTimeChangedListener {private TextView textView; private DatePicker datePicker; private TimePicker timePicker; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); datePicker = (DatePicker) findViewById (R. id. datepicker); textView = (TextView) findViewById (R. id. textview); timePicker = (TimePicker) findViewById (R. id. timepicker); datePicker. init (2015, 4, 24, this); // initialization date timePicker. setIs24HourView (true); // whether to display timePicker in 24-hour format. setOnTimeChangedListener (this); // register event} // trigger of the time control @ Overridepublic void onTimeChanged (TimePicker view, int hourOfDay, int minute) {// TODO Auto-generated method stubToast. makeText (MainActivity. this, "hourOfDay" + hourOfDay + "minute" + minute, 1 ). show ();} // Date control trigger @ Overridepublic void onDateChanged (DatePicker view, int year, int monthOfYear, int dayOfMonth) {// TODO Auto-generated method stubCalendar calendar = Calendar. getInstance (); calendar. set (datePicker. getYear (), datePicker. getMonth (), datePicker. getDayOfMonth (), timePicker. getCurrentHour (), timePicker. getCurrentMinute (); SimpleDateFormat format = new SimpleDateFormat ("MM dd, yyyy HH: mm"); textView. setText (format. format (calendar. getTime ()));}}
Activity_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.datepickerdemo.MainActivity" > <DatePicker android:id="@+id/datepicker" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <TimePicker android:id="@+id/timepicker" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <TextView android:id="@+id/textview" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textSize="18dp" /></LinearLayout>