【Android】时间与日期Widget(DatePicker 与 TimePicker)
2011-12-26 19:15
956 查看
public class
extends FrameLayout
This class is a widget for selecting a date. The date can be selected by a year, month, and day spinners or a
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.
To provide a widget for selecting a date, use the
which allows the user to select the month, day, and year, in a familiar interface.
In this tutorial, you'll create a
the date picker in a floating dialog box at the press of a button. When the date is set by the user, a
update with the new date.
public class
extends FrameLayout
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
See the Time Picker tutorial.
下面是一个转载自书上的示例:
DatePicker
extends FrameLayoutjava.lang.Object | ||||
↳ | android.view.View | |||
↳ | android.view.ViewGroup | |||
↳ | android.widget.FrameLayout | |||
↳ | android.widget.DatePicker |
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.
To provide a widget for selecting a date, use the
DatePickerwidget,
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
TextViewwill
update with the new date.
public class
TimePicker
extends FrameLayoutjava.lang.Object | ||||
↳ | android.view.View | |||
↳ | android.view.ViewGroup | |||
↳ | android.widget.FrameLayout | |||
↳ | android.widget.TimePicker |
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 byhitting 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.
下面是一个转载自书上的示例:
public class Activity01 extends Activity { TextView m_TextView; //声明dataPicker DatePicker m_DatePicker; //声明TimePicker TimePicker m_TimePicker; Button m_dpButton; Button m_tpButton; //java中的Calendar类 Calendar c; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); c=Calendar.getInstance(); m_TextView= (TextView) findViewById(R.id.TextView01); m_dpButton = (Button)findViewById(R.id.button1); m_tpButton = (Button)findViewById(R.id.button2); //获取DataPicker对象 m_DatePicker = (DatePicker) findViewById(R.id.DatePicker01); //初始化当前时间并设置监听 m_DatePicker.init(c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH), new DatePicker.OnDateChangedListener() { @Override public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) { //当日期更改在这里设置 c.set(year, monthOfYear, dayOfMonth); } }); // m_TimePicker = (TimePicker) findViewById(R.id.TimePicker01); // m_TimePicker.setIs24HourView(true); // m_TimePicker.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() { @Override public void onTimeChanged(TimePicker view, int hourOfDay, int minute) { //当时间更改在这里设置 c.set(year, month, day, hourOfDay, minute, second); } }); m_dpButton.setOnClickListener(new Button.OnClickListener(){ public void onClick(View v) { new DatePickerDialog(Activity01.this, new DatePickerDialog.OnDateSetListener() { public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { //设置日历 } },c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH)).show(); } }); m_tpButton.setOnClickListener(new Button.OnClickListener() { public void onClick(View v) { new TimePickerDialog(Activity01.this, new TimePickerDialog.OnTimeSetListener() { public void onTimeSet(TimePicker view, int hourOfDay,int minute) { //设置时间 } },c.get(Calendar.HOUR_OF_DAY), c.get(Calendar.MINUTE), true).show(); } }); } }
对应的layout文件
<?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/TextView1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <Button android:id="@+id/Button1" android:layout_width="wrap_content" android:layout_height="wrap_content" > </Button> <Button android:id="@+id/Button2" android:layout_width="wrap_content" android:layout_height="wrap_content" > </Button> </LinearLayout>
相关文章推荐
- Android日期时间选择器DatePicker、TimePicker日期时间改变事件响应(Android学习笔记)
- Android中实现日期时间选择器(DatePicker和TimePicker)
- Android DatePicker与TimePicker 日期时间弹出工具类
- android学习:日期与时间(DatePicker和TimePicker)
- 第11章、日期DatePicker与时间TimePicker控件(从零开始学Android)
- 在android中用TimePicker和DatePicker或者TimePickerDialog和DatePickerDialog来动态调整时间和日期
- android 控件 日期和时间(DatePicker,TimePicker)
- 玩转Android---UI篇---DatePicker,TimePicker(日期和时间)
- android应用开发揭秘examples_04-11笔记(DatePicker,TimePicker时间日期设置,略有改动)
- Android练习-----日期选择器(DatePicker) && 时间选择器(TimePicker)
- 在Android EditText中实现日期时间选择器(DatePicker和TimePicker)
- android 时间日期控件的使用(DatePicker、TimePicker、DatePickerDialog、TimePickerDialog、AnalogClock、DigitalClock)
- Android中实现日期时间选择器(DatePicker和TimePicker)
- Android开发之日期、时间选择器(DatePicker和TimePicker)的功能和用法
- 第11章、日期DatePicker与时间TimePicker控件(从零开始学Android)
- Android中实现日期时间选择器(DatePicker和TimePicker)
- Android时间日期拾取器学习使用(DatePicker、TimePicker)
- Android之日期时间选择控件DatePicker和TimePicker
- Android中实现日期时间选择器(DatePicker和TimePicker)
- Android UI控件系列:DatePicker,TimePicker(日期和时间选择)