---
title: "【Android】时间与日期Widget（DatePicker 与 TimePicker)"
author: "mmdev"
date: "2011-12-26T19:15:00Z"
tags: []
slug: "android-widget-datepicker-timepicker"
draft: false
lastmod: "2012-05-07T21:19:25Z"
id: 1515274
url: "https://mmdev.iteye.com/blog/1515274"
---

public class

# DatePicker

extends[FrameLayout](http://developer.android.com/reference/android/widget/FrameLayout.html)

[java.lang.Object](http://developer.android.com/reference/java/lang/Object.html) ↳ [android.view.View](http://developer.android.com/reference/android/view/View.html) ↳ [android.view.ViewGroup](http://developer.android.com/reference/android/view/ViewGroup.html) ↳ [android.widget.FrameLayout](http://developer.android.com/reference/android/widget/FrameLayout.html) ↳ android.widget.DatePicker

![](http://hi.csdn.net/attachment/201112/26/0_1324897492dZZD.gif)

## Class Overview

This class is a widget for selecting a date. The date can be selected by a year, month, and day spinners or a`CalendarView`. The set of spinners and the calendar view are automatically synchronized. The client can customize whether only the spinners, or only the calendar view, or both to be displayed. Also the minimal and maximal date from which dates to be selected can be customized.

See the[Date Picker tutorial](http://developer.android.com/resources/tutorials/views/hello-datepicker.html).

To provide a widget for selecting a date, use the`DatePicker`widget, which allows the user to select the month, day, and year, in a familiar interface.

In this tutorial, you'll create a`DatePickerDialog`, which presents the date picker in a floating dialog box at the press of a button. When the date is set by the user, a`TextView`will update with the new date.

public class

# TimePicker

extends[FrameLayout](http://developer.android.com/reference/android/widget/FrameLayout.html)

[java.lang.Object](http://developer.android.com/reference/java/lang/Object.html) ↳ [android.view.View](http://developer.android.com/reference/android/view/View.html) ↳ [android.view.ViewGroup](http://developer.android.com/reference/android/view/ViewGroup.html) ↳ [android.widget.FrameLayout](http://developer.android.com/reference/android/widget/FrameLayout.html) ↳ android.widget.TimePicker

![](http://hi.csdn.net/attachment/201112/26/0_132489779689p5.gif)

## Class Overview

A view for selecting the time of day, in either 24 hour or AM/PM mode. The hour, each minute digit, and AM/PM (if applicable) can be conrolled by vertical spinners. The hour can be entered by keyboard input. Entering in two digit hours can be accomplished by hitting two digits within a timeout of about a second (e.g. '1' then '2' to select 12). The minutes can be entered by entering single digits. Under AM/PM mode, the user can hit 'a', 'A", 'p' or 'P' to pick. For a dialog using this view, see`TimePickerDialog`.

See the[Time Picker tutorial](http://developer.android.com/resources/tutorials/views/hello-timepicker.html).

下面是一个转载自书上的示例：

```

```

```

```

```

```

```

```