Home>

Android date and time use

Use of date and time;

1:popup boxes timepickerdialog, datepickerdialog

2:components timepicker, datepicker

Use of timepickerdialog:Figure 1 is displayed by clicking the button.The user can then set the time

The use of datepickerdialog only needs to modify timepickerdialog to datepickerdialog, timepickerdialog.ontimesetlistener to datepickerdialog, ondatesetlistener

public static class timepickerfragment extends dialogfragment
              implements timepickerdialog.ontimesetlistener {
    // Method for user to create popup time box
  @override
  public dialog oncreatedialog (bundle savedinstancestate) {
    // use the current time as the default values ​​for the picker
    final calendar c=calendar.getinstance ();
    int hour=c.get (calendar.hour_of_day);
    int minute=c.get (calendar.minute);
    // create a new instance of timepickerdialog and return it
    return new timepickerdialog (getactivity (), this, hour, minute,        dateformat.is24hourformat (getactivity ()));
  }
  public void ontimeset (timepicker view, int hourofday, int minute) {
    // do something with the time chosen by the user
  }
}
android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="@ string/pick_time"
  android:onclick="showtimepickerdialog" />
public void showtimepickerdialog (view v) {
  dialogfragment newfragment=new timepickerfragment ();
  newfragment.show (getsupportfragmentmanager (), "timepicker");
}

datepickerdialog code:

public class datepickerfragment extends dialogfragment implements datepickerdialog.ondatesetlistener {
  // The time method for the user to create the date dialog
  @override
  public dialog oncreatedialog (bundle savedinstancestate) {
    calendar c=calendar.getinstance ();
    int year=c.get (calendar.year);
    int month=c.get (calendar.month);
    int day=c.get (calendar.day_of_month);
    datepickerdialog dialog=new datepickerdialog (getactivity (), this, year, month, day);
    return dialog;
  }
  @override
  public void ondateset (datepicker view, int year, int monthofyear, int dayofmonth) {
  }
}
  • Previous Click the input with the mouse to display the instant border color, modify and hide the instance
  • Next Implementation method of Swift shuffle animation effect