我想让用户在今天之后选择日期,作为提醒日期,我该怎么做?另外,如何将主题放在这些代码中,因为没有按钮供用户选择。 / p>
以下是我的代码与datapickerdialog
和timepickerdialog
结合使用:
private void setDateTimeField (){
DatePickerDialog datePickerDialog = new DatePickerDialog(this, new
DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker datePicker, int selectedYear, int selectedMonth, int selectedDate) {
year = selectedYear;
month = selectedMonth;
day = selectedDate;
timePicker();
date_time = year + "-" + (month + 1) + "-" + day;
}
},year, month, day);
final Calendar calendar = Calendar.getInstance();
Calendar cal = Calendar.getInstance();
int yy = calendar.get(Calendar.YEAR);
int mm = calendar.get(Calendar.MONTH);
int dd = calendar.get(Calendar.DAY_OF_MONTH);
cal.set(Calendar.MONTH, mm);
cal.set(Calendar.DAY_OF_MONTH, dd);
cal.set(Calendar.YEAR, yy);
datePickerDialog.getDatePicker().setMinDate(cal.getTimeInMillis());
datePickerDialog.show();
}
private void timePicker(){
// Get Current Time
final Calendar c = Calendar.getInstance();
hours = c.get(Calendar.HOUR_OF_DAY);
minutes = c.get(Calendar.MINUTE);
// Launch Time Picker Dialog
TimePickerDialog timePickerDialog = new TimePickerDialog(this,new
TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int
minute) {
hours = hourOfDay;
minutes = minute;
modifytime.setText(date_time+" "+hourOfDay + ":" +
minute);
}
}, hours, minutes, false);
timePickerDialog.show();
}
答案 0 :(得分:1)
如果您想将当前日期设置为最短日期,并且不应选择过去的日期,那么以下解决方案将起作用我相信
Calendar cal = Calendar.getInstance();
int yy = calendar.get(Calendar.YEAR);
int mm = calendar.get(Calendar.MONTH);
int dd = calendar.get(Calendar.DAY_OF_MONTH);
//Min date setting part
cal.set(Calendar.MONTH, mm);
cal.set(Calendar.DAY_OF_MONTH, dd);
cal.set(Calendar.YEAR, yy);
//Replace your code mDatePickerDialog.getDatePicker().setMaxDate(System.currentTimeMillis()); with the below line
mDatePickerDialog.setMinDate(cal.getTimeInMillis());
由于您允许用户仅选择将来的日期,因此您无需设置最长日期。
完整的代码示例供您参考 -
private void showDateDailog() {
final DatePickerDialog datePickerDialog = new DatePickerDialog(getApplicationContext(), new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker datePicker, int selectedYear, int selectedMonth, int selectedDate) {
year = selectedYear;
month = selectedMonth;
day = selectedDate;
addtime.setText(new StringBuilder().append(year).append("/")
.append(month + 1).append("/").append(day));
}
}, year, month, day);
final Calendar calendar = Calendar.getInstance();
Calendar cal = Calendar.getInstance();
int yy = calendar.get(Calendar.YEAR);
int mm = calendar.get(Calendar.MONTH);
int dd = calendar.get(Calendar.DAY_OF_MONTH);
//Min date setting part
cal.set(Calendar.MONTH, mm);
cal.set(Calendar.DAY_OF_MONTH, dd);
cal.set(Calendar.YEAR, yy);
datePickerDialog.setMinDate(cal.getTimeInMillis());
//Maximum date setting part, if you need (Else don't add it)
/*Calendar calen = Calendar.getInstance();
calen.set(Calendar.MONTH, mm);
calen.set(Calendar.DAY_OF_MONTH, dd);
calen.set(Calendar.YEAR, yy + 2);
datePickerDialog.setMaxDate(calen.getTimeInMillis());*/
datePickerDialog.show();
}