Home>

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

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 ();
 //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 ()
 {
 @override
 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
 @override
 public 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 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 () {
  @override
  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
  @override
  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