Android --- 29 --- DatePicker, TimePicker, and androidtimepicker

Source: Internet
Author: User

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>




Related Article

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.