DatePicker继承自FrameLayout类,日期选择控件的主要功能是向用户提供包含年、月、日的日期数据并允许用户对其修改。如果要捕获用户修改日期选择控件中的数据事件,需要为DatePicker添加OnDateChangedListener监听器。 TimePicker也继承自FrameLayout类。时间选择控件向用户显示一天中的时间(可以为24小时,也可以为AM/PM制),并允许用户进行选择。如果要捕获用户修改时间数据的事件,便需要为TimePicker添加OnTimeChangedListener监听器。

使用时间和日期控件,免去判断输入时间合法性。同时也能提高用户体验。下面模拟日期和时间控件的用法。

所先我们看一下DatePicker的使用,在下面这个例子中,有一个DatePicker控件,并添加了OnDateChangedListener监听器,当日期改变的时候,当前Activity的标题也随着改变。

所以看一下布局文件,只是添加了一个DatePicker控件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
     <DatePicker
         android:id="@+id/mDatePicker"
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"/>
</LinearLayout>

然后看一下代码实现:

package com.liuzhichao.widget;

import java.util.Calendar;

import android.app.Activity;
import android.os.Bundle;
import android.widget.DatePicker;
import android.widget.DatePicker.OnDateChangedListener;

public class DatePickerActivity extends Activity {

	private DatePicker mDatePicker;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		setContentView(R.layout.datepicker);
		mDatePicker = (DatePicker) findViewById(R.id.mDatePicker);

		//Calendar是设定年度日期对象和一个整数字段之间转换的抽象基类
		//获取当前年月日
		Calendar calendar = Calendar.getInstance();
		int year = calendar.get(Calendar.YEAR);
		int monthOfYear = calendar.get(Calendar.MONTH);
		int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);

		/**
		 *  init (int year, int monthOfYear, int dayOfMonth,
		 *   DatePicker.OnDateChangedListener onDateChangedListener)
		 *   将日历初始化为当前系统时间,并设置其事件监听
		 */
		mDatePicker.init(year, monthOfYear, dayOfMonth,
				new OnDateChangedListener() {
					public void onDateChanged(DatePicker view, int year,
							int monthOfYear, int dayOfMonth) {
						//设置标题,注意月份是从0开始的,所以我们在显示的时候要+1
					setTitle("您选择的日期是:"+year+"年"+(monthOfYear+1)+"月"+dayOfMonth+"日");
					}
				});
	}

}

运行效果如下:

DatePicker
DatePicker

下面我们再来看一下TimePicker的使用。TimePicker的使用与DatePicker类似。但有些时候我们对分钟要求以15分钟为最小单位选择该如何实现呢?首先我们的布局文件中有两个TimePicker控件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

     <TimePicker
         android:id="@+id/mTimPicker"
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"/>

     <TimePicker
         android:id="@+id/mTimPicker2"
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"/>
</LinearLayout>

然后我们看一下程序实现:

package com.liuzhichao.widget;

import java.lang.reflect.Field;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TimePicker;
import android.widget.TimePicker.OnTimeChangedListener;
import android.widget.Toast;

public class TimePickerActivity extends Activity {

	private TimePicker mTimePicker;
	private TimePicker mTimePicker2;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.timepicker);
		mTimePicker = (TimePicker)findViewById(R.id.mTimPicker);
		mTimePicker2  = (TimePicker)findViewById(R.id.mTimPicker2);


		mTimePicker.setIs24HourView(true);//是否显示24小时制?默认false
		mTimePicker.setCurrentHour(5);     //设置当前小时
		mTimePicker.setCurrentMinute(20); //设置当前分钟

		mTimePicker.setOnTimeChangedListener(new OnTimeChangedListener() {

			public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
				Toast.makeText(TimePickerActivity.this, "您设置的时间为:"+hourOfDay+":"+minute, 1).show();
			}
		});

		//设置mTimePicker2为24小时制
		mTimePicker2.setIs24HourView(true);
		//设置mTimePicker2初始值为5
		mTimePicker2.setCurrentMinute(5);


		/**
		 * 设置mTimePicker2时间改变事件处理
		 * 时间只能取15、30、45
		 */
		mTimePicker2.setOnTimeChangedListener(new OnTimeChangedListener() {
			public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
				 try {

					Field mMinutePicker = view.getClass().getDeclaredField("mMinutePicker");
					mMinutePicker.setAccessible(true);

					Object value = mMinutePicker.get(view);

					Field mCurrent = value.getClass().getDeclaredField("mCurrent");
					mCurrent.setAccessible(true);

					Field mCurrentMinute = view.getClass().getDeclaredField("mCurrentMinute");
					mCurrentMinute.setAccessible(true);

					 if(minute>0&&minute<15){
	                     mCurrent.set(value, 15);
	                     mCurrentMinute.set(view, 15);
	                  }else if (minute>15&&minute<30) {
	                	  mCurrent.set(value, 30);
		                  mCurrentMinute.set(view, 30);
					 }else if (minute>30&&minute<45) {
						  mCurrent.set(value, 45);
		                  mCurrentMinute.set(view, 45);
					 }else{
	                     mCurrent.set(value, 0);
	                     mCurrentMinute.set(view, 0);
	                  }

				} catch (SecurityException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (NoSuchFieldException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IllegalArgumentException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IllegalAccessException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}
		});
	}

}

再来看一下运行效果,注意,在第二个TimePicker中,分钟只能是15、30、45.

TimePicker
TimePicker