I am trying to make something like a reminder app. I want to allow the user to select date and time that is not now (at least 5 minutes from now), and I also want to disable the user from selecting a date that is too far away - 30 days for example. I created datePicker and timePicker, made them pop up on button click, but could not find way to set the min and max values.
public void showDateDialog() {
btnDate = (Button) findViewById(R.id.buttonDate);
btnDate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showDialog(DATE_DIALOG_ID);
}
});
}
public void showTimeDialog() {
btnTime = (Button) findViewById(R.id.buttonTime);
btnTime.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showDialog(TIME_DIALOG_ID);
}
});
}
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DATE_DIALOG_ID:
// create a new DatePickerDialog with values you want to show
return new DatePickerDialog(this, datePickerListener, yearSet, monthSet, daySet);
// create a new TimePickerDialog with values you want to show
case TIME_DIALOG_ID:
return new TimePickerDialog(this, timePickerListener, hourSet, minuteSet, false);
}
return null;
}
private DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
yearSet = year;
monthSet = monthOfYear + 1;
daySet = dayOfMonth;
btnDate.setText(new SimpleDateFormat("DD").format(daySet) + "-" + new SimpleDateFormat("MMM").format(monthSet) + "-" + yearSet);
}
};
private TimePickerDialog.OnTimeSetListener timePickerListener = new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hour, int minute) {
hourSet = hour;
minuteSet = minute;
btnTime.setText(hourSet + ":" + new SimpleDateFormat("MM").format(hourSet));
}
};
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…