ListPreferenceのようなカスタムビューを作成し、このソリューションを実装しました。
private TimePickerDialog.OnTimeSetListener mTimeSetListener = new TimePickerDialog.OnTimeSetListener() {
public void onTimeSet(TimePicker view, int hourOfDay, int minuteOfHour) {
mHour = hourOfDay;
mMinute = minuteOfHour;
updateDisplay();
}
};
DatePickerDialog.OnDateSetListener mDateSetListener =
new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
mYear = year;
mMonth = monthOfYear;
mDay = dayOfMonth;
updateDisplay();
}
};
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DATE_DIALOG_ID:
return new DatePickerDialog(this,
mDateSetListener,
mYear, mMonth, mDay);
case TIME_DIALOG_ID:
return new TimePickerDialog(this, mTimeSetListener, mHour, mMinute,
false);
}
return null;
}
protected void onPrepareDialog(int id, Dialog dialog) {
switch (id) {
case DATE_DIALOG_ID:
((DatePickerDialog) dialog).updateDate(mYear, mMonth, mDay);
break;}
}
private void updateDisplay() {
String minutes = null;
if (mMinute>9)
minutes = Integer.toString(mMinute);
else
minutes = "0"+Integer.toString(mMinute);
if (mMonth<10)
deadlineCus.setSummary(
(new StringBuilder()
// Month is 0 based so add 1
.append(mYear).append("-").append("0")
.append(mMonth + 1).append("-")
.append(mDay)
.append(" ").append(mHour).append(":").append(minutes)).toString()
);
else
deadlineCus.setSummary(
(new StringBuilder()
// Month is 0 based so add 1
.append(mYear).append("-")
.append(mMonth + 1).append("-")
.append(mDay)
.append(" ").append(mHour).append(":").append(minutes)).toString()
);
showDialog(TIME_DIALOG_ID);
}
onCreate()メソッドの場合:
DatePickerDialog.OnDateSetListener mDateSetListener =
new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
mYear = year;
mMonth = monthOfYear;
mDay = dayOfMonth;
updateDisplay();
}
};
TimePickerDialog.OnTimeSetListener mTimeSetListener = new TimePickerDialog.OnTimeSetListener() {
public void onTimeSet(TimePicker view, int hourOfDay, int minuteOfHour) {
mHour = hourOfDay;
mMinute = minuteOfHour;
updateDisplay();
}
};
deadlineCus.setOnTouchListener(new OnTouchListener()
{
public boolean onTouch(View arg0, MotionEvent arg1)
{
showDialog(DATE_DIALOG_ID);
return false;
}
});