Android------DatePicker, timepick

Source: Internet
Author: User







DatePicker and Timepicker are controls that are used to display time.


Ondatechanged, ontimechanged are trigger events for controls, respectively.


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 Time controls * * @author Caesar * */public class Mainactivity extends Activity implements Ondatechangedlistener,ontimechang Edlistener {private TextView textview;private DatePicker datepicker;private timepicker timepicker; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_ Main);d Atepicker = (datePicker) Findviewbyid (r.id.datepicker); TextView = (TextView) Findviewbyid (R.id.textview); Timepicker = (timepicker) Findviewbyid (r.id.timepicker);d Atepicker.init (4, this);//Initialize Date TimepicKer.setis24hourview (TRUE);//Displays whether the time is in accordance with the 24-hour system Timepicker.setontimechangedlistener (this);//Register Event}// Trigger of the time control @overridepublic void ontimechanged (timepicker view, int hourofday, int minute) {//TODO auto-generated method Stubt Oast.maketext (Mainactivity.this, "hourofday" + hourofday + "minute" + minute, 1). Show (); Trigger of the date control @overridepublic void ondatechanged (DatePicker view, int year, int monthofyear,int dayofmonth) {//TODO Auto-gene Rated method Stubcalendar calendar = Calendar.getinstance (); Calendar.set (Datepicker.getyear (), Datepicker.getmonth () , Datepicker.getdayofmonth (), Timepicker.getcurrenthour (), Timepicker.getcurrentminute ()); SimpleDateFormat format = new SimpleDateFormat ("yyyy mm month DD Day 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:orienta tion= "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_h       eight= "Wrap_content" android:textsize= "18DP" /></linearlayout> 




Android------DatePicker, timepick

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.