參考代碼:
/**?*?彈出日期對話框
?*?@param?textView
?*?@param?date?默認日期
?*/
public?void?showDateDialog(TextView?textView,?String?date)?{
txtDatePicker?=?textView;
calendar?=?Calendar.getInstance();
if?(!toolUtil.isBlank(date))?{
try?{calendar.setTime(dateUtil.formatDate().parse(date));}?catch?(Exception?e)?{}
}
new?DatePickerDialog(this,?new?DatePickerDialog.OnDateSetListener()?{
public?void?onDateSet(DatePicker?view,?int?year,?int?monthOfYear,?int?dayOfMonth)?{
calendar.set(Calendar.YEAR,?year);
calendar.set(Calendar.MONTH,?monthOfYear);
calendar.set(Calendar.DAY_OF_MONTH,?dayOfMonth);
txtDatePicker.setText(dateUtil.formatDate().format(calendar.getTime()));
}
},?calendar.get(Calendar.YEAR),?calendar.get(Calendar.MONTH),?calendar.get(Calendar.DAY_OF_MONTH)).show();
}
/**
?*?彈出時間對話框
?*?@param?textView
?*/
public?void?showTimeDialog(TextView?textView)?{
txtDatePicker?=?textView;
new?TimePickerDialog(this,?new?TimePickerDialog.OnTimeSetListener()?{
public?void?onTimeSet(TimePicker?view,?int?hourOfDay,?int?minute)?{
calendar.set(Calendar.HOUR_OF_DAY,?hourOfDay);
calendar.set(Calendar.MINUTE,?minute);
txtDatePicker.setText((hourOfDay?<?10"0"?+?hourOfDay?:?hourOfDay)?+?":"?+?(minute?<?10"0"?+?minute?:?minute)?+?":00");
}
},?calendar.get(Calendar.HOUR_OF_DAY),?calendar.get(Calendar.MINUTE),?true).show();
}