日期选择器(DatePicker)和时间选择器(TimePicker)的功能与用法

DatePicker和TimePicker都从FrameLayout派生而来,其中DatePicker供用户选择日期,而TimePicker则供用户选择时间。它们在FrameLayout的基础上提供了一些方法来获取当前用户所选择的日期和时间,如果程序需要获取用户所选择的日期和时间,则可通过为DatePicker添加OnDateChangeListener进行监听、为TimePicker添加OnTimeChangeListener进行监听来实现。

看看以下代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    
    <TextView 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="选择购买本书的具体时间"/>
    
    <!-- 定义一个DatePicker组件 -->
	<DatePicker 
	    android:id="@+id/dap"
	    android:layout_width="wrap_content"
	    android:layout_height="wrap_content"
	    android:layout_gravity="center_horizontal"/>
	
	<!-- 定义一个TimePicker组件 -->
	<TimePicker 
	    android:id="@+id/tip"
	    android:layout_width="wrap_content"
	    android:layout_height="wrap_content"
	    android:layout_gravity="center_horizontal"/>
	
	<!-- 显示用户输入日期、时间的控件 -->
	<EditText 
	    android:id="@+id/show"
	    android:layout_width="match_parent"
	    android:layout_height="wrap_content"
	    android:editable="false"
	    android:cursorVisible="false"/>


</LinearLayout>

上面界面布局中除添加了一个DatePicker和一个TimePicker供用户选择日期和时间外,还包含一个EditText,用于显示用户选择的日期和时间。

import java.util.Calendar;

import android.app.Activity;
import android.os.Bundle;
import android.widget.DatePicker;
import android.widget.DatePicker.OnDateChangedListener;
import android.widget.EditText;
import android.widget.TimePicker;
import android.widget.TimePicker.OnTimeChangedListener;

public class DatePickerActivity extends Activity {

	private int year;
	private int month;
	private int day;
	private int hour;
	private int minute;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.datepicker);
		DatePicker datepicker = (DatePicker) findViewById(R.id.dap);
		TimePicker timepicker = (TimePicker) findViewById(R.id.tip);
		// 获取当前的年、月、日、小时、分钟
		Calendar c = Calendar.getInstance();
		year = c.get(Calendar.YEAR);
		month = c.get(Calendar.MONTH);
		day = c.get(Calendar.DAY_OF_MONTH);
		hour = c.get(Calendar.HOUR);
		minute = c.get(Calendar.MINUTE);
		// 初始化DatePicker组件,初始化时指定监听器
		datepicker.init(year, month, day, new OnDateChangedListener() {

			@Override
			public void onDateChanged(DatePicker arg0, int year, int month,
					int day) {
				// TODO Auto-generated method stub
				DatePickerActivity.this.year = year;
				DatePickerActivity.this.month = month;
				DatePickerActivity.this.day = day;
				// 显示当前的日期、时间
				showDate(year, month, day, hour, minute);
			}

		});
		// 为TimePicker指定监听吕
		timepicker.setOnTimeChangedListener(new OnTimeChangedListener() {

			@Override
			public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
				// TODO Auto-generated method stub
				DatePickerActivity.this.hour = hourOfDay;
				DatePickerActivity.this.minute = minute;
				//显示当前日期、时间
				showDate(year, month, day, hour, minute);
			}

		});
	}
	//定义在EditText中显示当前日期、时间的方法
	private void showDate(int year, int month, int day, int hour, int minute) {
		// TODO Auto-generated method stub
		EditText show = (EditText) findViewById(R.id.show);
		show.setText("您购买的日期为:" + year + "年" + (month + 1) + "月" + day + "日"
				+ hour + "时" + minute + "分");
	}
}

相关推荐