1. Date setting control:datepickerdialog

2. Time setting control:timepickerdialog

Example code:

Add two buttons on the page, click to display the date setting control and time setting control,There is still a textview control for displaying the set system time


<?xml version="1.0" encoding="utf-8"?>
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
<textview android:id="@ + id/dateandtime"
 android:text="@ string/hello"
 android:id="@ + id/setdate"
 android:text="set the date"></button>
<button android:id="@ + id/settime"
 android:text="set the time"></button>

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 ();
 //Define a textview control object
 textview dateandtimelabel=null;
 //Get a calendar object
 calendar dateandtime=calendar.getinstance (locale.china);
 //When the setting button of the datepickerdialog control is clicked,Call the method
 datepickerdialog.ondatesetlistener d=new datepickerdialog.ondatesetlistener ()
 public void ondateset (datepicker view, int year, int monthofyear, int dayofmonth) {
  //Modify the year of the calendar control,Month Day
  //The values ​​of year, monthofyear, and dayofmonth are 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);
  //Update the display of the page textview to the latest time
  updatelabel ();
 timepickerdialog.ontimesetlistener t=new timepickerdialog.ontimesetlistener () {
 //Same as datepickerdialog control
 public void ontimeset (timepicker view, int hourofday, int minute) {
  dateandtime.set (calendar.hour_of_day, hourofday);
  dateandtime.set (calendar.minute, minute);
  updatelabel ();
 public void oncreate (bundle savedinstancestate) {
 super.oncreate (savedinstancestate);
 setcontentview (r.layout.main);
 //Get the button control object of the page set date
 button datebtn=(button) findviewbyid (r.id.setdate);
 //Set the click event listener for the button
 datebtn.setonclicklistener (new view.onclicklistener () {
  public void onclick (view v) {
  //Generate a datepickerdialog object and display it. The datepickerdialog control displayed can choose year, month, and day.
And set
  new datepickerdialog (chronodemo.this,   d,   dateandtime.get (calendar.year),   dateandtime.get (calendar.month),   dateandtime.get (calendar.day_of_month)). show ();
 button timebtn=(button) findviewbyid (r.id.settime);
 timebtn.setonclicklistener (new view.onclicklistener () {
  //Same as above
  public void onclick (view v) {
  new timepickerdialog (chronodemo.this,   t,   dateandtime.get (calendar.hour_of_day),   dateandtime.get (calendar.minute),   true) .show ();
 dateandtimelabel=(textview) findviewbyid (r.id.dateandtime);
 updatelabel ();
 //Method of updating page textview
 private void updatelabel () {
 dateandtimelabel.settext (fmtdateandtime
 .format (dateandtime.gettime ()));
  • Previous Java method for developing Unicom short message based on SGIP protocol
  • Next Tutorial for creating dynamic views in Django