Android ApiDemos示例解析(110):Views->Data Widgets->1. Dialog
上一篇:http://www.zzzyk.com/kf/201208/146229.html
本例介绍了日期DatePickerDialog和时间对话框TimePickerDialog的用法,可以获取时间或日期输入。
DatePickerDialog和TimePickerDialog都是AlertDialog的子类,因此和Alert的使用步骤基本一致: Android ApiDemos示例解析(22):App->Dialog
1. 定义存储日期和时间的变量
[java]
// date and time
private int mYear;
private int mMonth;
private int mDay;
private int mHour;
private int mMinute;
// date and time
private int mYear;
private int mMonth;
private int mDay;
private int mHour;
private int mMinute;
2. 为日期和时间对话框定义ID
[java]
static final int TIME_DIALOG_ID = 0;
static final int DATE_DIALOG_ID = 1;
static final int TIME_DIALOG_ID = 0;
static final int DATE_DIALOG_ID = 1;
3. 在Activity的onCreateDialog事件中初始化对话框
[java]
switch (id) {
case TIME_DIALOG_ID:
return new TimePickerDialog(this,
mTimeSetListener, mHour, mMinute, false);
case DATE_DIALOG_ID:
return new DatePickerDialog(this,
mDateSetListener,
mYear, mMonth, mDay);
}
switch (id) {
case TIME_DIALOG_ID:
return new TimePickerDialog(this,
mTimeSetListener, mHour, mMinute, false);
case DATE_DIALOG_ID:
return new DatePickerDialog(this,
mDateSetListener,
mYear, mMonth, mDay);
}
4. 应用中可以多次显示时间或日期对话框,前面在onCreateDialog初始化对话框,在每次重新显示对话框前,可以在onPrepareDialog 重新设置对话框的值,本例使对话框显示当前屏幕显示的时间和日期值。
[java]
switch (id) {
case TIME_DIALOG_ID:
((TimePickerDialog) dialog)
.updateTime(mHour, mMinute);
break;
case DATE_DIALOG_ID:
((DatePickerDialog) dialog)
.updateDate(mYear, mMonth, mDay);
break;
}
switch (id) {
case TIME_DIALOG_ID:
((TimePickerDialog) dialog)
.updateTime(mHour, mMinute);
break;
case DATE_DIALOG_ID:
((DatePickerDialog) dialog)
.updateDate(mYear, mMonth, mDay);
break;
}
5. 构造函数定义
本例TimePickerDialog使用的构造函数定义如下:
public TimePickerDialog(Context context,
TimePickerDialog.OnTimeSetListener callBack,
int hourOfDay,
int minute, boolean is24HourView)
hourOfDay 小时
int minute 分钟
is24HourView 是否采用24小时制
callback ,用户选择好时间后,通知应用的回调函数,其类型为TimePickerDialog.OnTimeSetListener,可以用来取得当前时间对话框中的时间:
[java]
private TimePickerDialog.OnTimeSetListener mTimeSetListener =
new TimePickerDialog.OnTimeSetListener() {
public void onTimeSet(TimePicker view,
int hourOfDay, int minute) {
mHour = hourOfDay;
mMinute = minute;
updateDisplay();
}
};
private TimePickerDialog.OnTimeSetListener mTimeSetListener =
new TimePickerDialog.OnTimeSetListener() {
public void onTimeSet(TimePicker view,
int hourOfDay, int minute) {
mHour = hourOfDay;
mMinute = minute;
updateDisplay();
}
};
DatePickerDialog 本例使用的构造函数如下:
[java]
public DatePickerDialog(Context context,
DatePickerDialog.OnDateSetListener callBack,
int year,
int monthOfYear,
int dayOfMonth)
public DatePickerDialog(Context context,
DatePickerDialog.OnDateSetListener callBack,
int year,
int monthOfYear,
int dayOfMonth)year 年
monthOfYear 月
dayOfMonth 日
callBack 用户选择好日期后,通知应用的回调函数,其类型为TimePickerDialog.OnTimeSetListener,可以用来取得当前时间对话框中的日期:
[java]
private TimePickerDialog.OnTimeSetListener mTimeSetListener =
new TimePickerDialog.OnTimeSetListener() {
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
mHour = hourOfDay;
mMinute = minute;
updateDisplay();
}
};
private TimePickerDialog.OnTimeSetListener mTimeSetListener =
new TimePickerDialog.OnTimeSetListener() {
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
mHour = hourOfDay;
mMinute = minute;
updateDisplay();
}
};
作者:mapdigit
补充:移动开发 , Android ,