「Android開発」DatePickerDialogとTimePickerDialogを利用する方法

利用方法
onCreateDialog、onPrepareDialog、mDateSetListener 、mTimeSetListener

サンプルコード
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case 1:
return new TimePickerDialog(this, mTimeSetListener, mHour, mMinute,
false);
case 2:
return new DatePickerDialog(this, mDateSetListener, mYear,
mMonth – 1, mDay);
}
return null;
}

//ダイアログボックス表示値を更新
@Override
protected void onPrepareDialog(int id, Dialog dialog) {
switch (id) {
case 1:
((TimePickerDialog) dialog).updateTime(mHour, mMinute);
break;
case 2:
((DatePickerDialog) dialog).updateDate(mYear, mMonth – 1, mDay);
break;
}
}
private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
mYear = year;
mMonth = monthOfYear + 1;
mDay = dayOfMonth;
setDatetime();
}
};
private TimePickerDialog.OnTimeSetListener mTimeSetListener = new TimePickerDialog.OnTimeSetListener() {
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
mHour = hourOfDay;
mMinute = minute;
setDatetime();
}
};

Android

Posted by arkgame