Android date and Time settings control usage Instance _android

Source: Internet
Author: User
Tags dateformat event listener locale

This example describes the use of date and time settings controls in Android. Share to everyone for your reference. Specifically as follows:

1. Date Setting Control: Datepickerdialog

2, Time settings control: Timepickerdialog

Instance code:

Page to add two button, click to display the Date setting control and the time setting control, or the TextView control to display the system time after setting

Main.xml:

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android=
"http://schemas.android.com/apk/" Res/android "
 android:orientation=" vertical "
 android:layout_width=" fill_parent "
 android:layout_" height= "Fill_parent"
 >
<textview android:id= "@+id/dateandtime" android:layout_width= "Fill_"
 Parent "
 android:layout_height=" wrap_content "
 android:text=" @string/hello "
 />
<button
 android:id= "@+id/setdate"
 Android:layout_width= "Fill_parent"
 android:layout_height= "wrap_content"
 android:text= "Set the Date" ></Button>
<button android:id= "@+id/settime"
 android:layout_width= "Fill_parent"
 android:layout_height= "Wrap_content"
 android:text= "Set the Time" ></Button>
</linearlayout >

Chronodemo.java is as follows:

Package Yyl.
Android;
Import Java.text.DateFormat;
Import Java.util.Calendar;
Import Java.util.Locale;
Import Android.app.Activity;
Import Android.app.DatePickerDialog;
Import Android.app.TimePickerDialog;
Import Android.os.Bundle;
Import Android.view.View;
Import Android.widget.Button;
Import Android.widget.DatePicker;
Import Android.widget.TextView;
Import Android.widget.TimePicker;
 public class Chronodemo extends activity {//Get Date Formatter object DateFormat fmtdateandtime = Dateformat.getdatetimeinstance ();
 Defines a TextView control object TextView Dateandtimelabel = null;
 Gets a calendar object in Calendar DateAndTime = Calendar.getinstance (Locale.china);
 Call this method Datepickerdialog.ondatesetlistener D = new Datepickerdialog.ondatesetlistener () when you click the Datepickerdialog control's Settings button. {@Override public void Ondateset (DatePicker view, int years, int monthofyear,int dayofmonth) {//Modify Calendar control year, month, day//y here
  The value of the ear,monthofyear,dayofmonth is consistent with the latest values set by the Datepickerdialog control Dateandtime.set (Calendar.year, year); Dateandtime.set (Calendar.moNth, monthofyear); 
  Dateandtime.set (Calendar.day_of_month, DayOfMonth);  
 Updates the display of page TextView to the latest time Updatelabel ();
 } 
 }; Timepickerdialog.ontimesetlistener t = new Timepickerdialog.ontimesetlistener () {//Datepickerdialog control @Override Publ
  IC void Ontimeset (timepicker view, int hourofday, int minute) {Dateandtime.set (calendar.hour_of_day, hourofday);
  Dateandtime.set (Calendar.minute, MINUTE);
 Updatelabel ();
 }
 };
 @Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
 Setcontentview (R.layout.main);
 Get the page Set Date button datebtn = (button) Findviewbyid (r.id.setdate); Set button click event Listener datebtn.setonclicklistener (new View.onclicklistener () {@Override public void OnClick (View v) {//Build A Datepickerdialog object and displays it. The Datepickerdialog control that is displayed can select the day of the month and set the new Datepickerdialog (Chronodemo.this, D, Dateandtime.get (calendar.year), DateA
  Ndtime.get (Calendar.month), Dateandtime.get (Calendar.day_of_month)). Show ();
 }
 }); ButtOn timebtn = (Button) Findviewbyid (r.id.settime); Timebtn.setonclicklistener (New View.onclicklistener () {//Ibid. principle @Override public void OnClick (View v) {new timepic Kerdialog (Chronodemo.this, T, Dateandtime.get (Calendar.hour_of_day), Dateandtime.get (Calendar.minute), true). s
  how ();
 }
 });
 Dateandtimelabel= (TextView) Findviewbyid (r.id.dateandtime);
 Updatelabel (); //Update page TextView method private void Updatelabel () {dateandtimelabel.settext (fmtdateandtime. Format Dateandtime.gettime (
 ))); }
}

I hope this article will help you with your Android program.

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.