How to use DatePicker and Timepicker in Android _android

Source: Internet
Author: User
Tags event listener time and date

This article describes the use of DatePicker and Timepicker in Android in an example, as follows:
The following is the code that implements the specific functionality, where main.xml code is:

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android= "http://schemas.android.com/apk/res/" Android "Android:layout_width=" Fill_parent "android:layout_height=" fill_parent "android:orientation=" vertical " > <textview android:id= "@+id/textview1" android:layout_width= wrap_content "android:layout_height=" WR Ap_content "android:text=" Please choose the time to buy this book "/> <datepicker android:id=" @+id/datepicker "Android:layout_wi" Dth= "Wrap_content" android:layout_height= "wrap_content" android:layout_gravity= "Center_horizontal"/> <Ti 
    Mepicker android:id= "@+id/timepicker" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" android:layout_gravity= "Center_horizontal"/> <edittext android:id= "@+id/show" android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" android:cursorvisible= "false" android:editable= "false"/&G

T
 </LinearLayout>

Java code is:

public class Androiddemo5activity extends activity {//log current time private int year;
 private int month;
 private int day;
 private int hour;

 private int minute; @Override protected void OnCreate (Bundle savedinstancestate) {//TODO auto-generated Method stub super.oncreate (Savedi
 Nstancestate);
 Setcontentview (R.layout.main);
 DatePicker date = (DatePicker) Findviewbyid (R.id.datepicker);
 Timepicker time = (timepicker) Findviewbyid (R.id.timepicker);
 Perhaps the current date of day, hour, minute Calendar CA = calendar.getinstance ();
 Year = Ca.get (calendar.year);
 month = Ca.get (calendar.month);
 Day = Ca.get (calendar.day_of_month);
 hour = Ca.get (Calendar.hour);

 minute = Ca.get (Calendar.minute); Initializes DatePicker Date.init (year, month, day, new Ondatechangedlistener () {@Override public void ondatechanged (datep
  Icker arg0, int year, int month, int day) {AndroidDemo5Activity.this.year = year;
  AndroidDemo5Activity.this.month = month;
  AndroidDemo5Activity.this.day = day; Show current time and date shOwdate (year, month, day, hour, minute);

 }

 }); Specifies the event listener Time.setontimechangedlistener for Timerpicker (new Ontimechangedlistener () {@Override public void Ontimechang
  Ed (timepicker arg0, int hour, int minute) {AndroidDemo5Activity.this.hour = hour;
  AndroidDemo5Activity.this.minute = minute;
 }
 }); } protected void Showdate (int year2, int month2, int day2, int hour2, int minute2) {edittext text = (edittext) findv
 Iewbyid (r.id.show);

 Text.settext ("Your purchase time is:" + year2 + "Year" + Month2 + "Month" + day2 + "Day" + HOUR2 + "Time" + Minute2 + "cent");

 }
}

The effect is as shown in the following illustration:

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.