我有以下显示DatePicker的函数,我试图设置允许的最小和最大日期。我成功地将最大日期设置为我之后的日期,但是我对最小日期没有运气。我希望将最小日期设置为明天,以便用户无法在对话框中选择今天的日期。
我做错了什么?
+----------+ +----------+
| WAR | | JAR-WS |
| +-------------------+ |
| | COMMONS | |
| +-------------------+ |
+----------+ +----------+
答案 0 :(得分:1)
像这样实施,
Calendar now = Calendar.getInstance();
DatePickerDialog dpd = DatePickerDialog.newInstance(
CreateAuction_Activity.this,
now.get(Calendar.YEAR),
now.get(Calendar.MONTH),
now.get(Calendar.DAY_OF_MONTH)
);
dpd.show(getFragmentManager(), "Datepickerdialog");
now.add(Calendar.DAY_OF_MONTH, +1);// This the line you are asking
dpd.setMinDate(now);
答案 1 :(得分:0)
错误表示您无法将最小日期设置为完全正确。尝试减去第二个:
datePicker.setMinDate(System.currentTimeMillis() - 1000);
从源代码开始,最小日期必须在当前日期之前,而不是等于:
if (date.before(mMinDate)) {
throw new IllegalArgumentException("fromDate: " + mMinDate.getTime()
+ " does not precede toDate: " + date.getTime());
}
所以你只需要从现在开始减去足够的时间(System.currentTimeMillis())传递date.before(mMinDate)。
答案 2 :(得分:0)
final Calendar cal = Calendar.getInstance();
day = cal.get(Calendar.DAY_OF_MONTH);
tomorrow=day+1;
tomorrow.setTimeInMillis(System.currentTimeMillis()+(1000 * 60 * 60 * 24));
datePickerDialog.setMinDate(tomorrow);
答案 3 :(得分:0)
使用此代码选择明天日期。刚刚在材料设计datepicker中进行了测试
int mYear,mMonth,mDay;
final Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
DatePickerDialog datePickerDialog=new DatePickerDialog(context, R.style.datepicker,new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
ETDate.setText(dayOfMonth + "-" + (month + 1) + "-" + year);
}
}, mYear, mMonth, mDay);
datePickerDialog.getDatePicker().setMinDate(c.getTimeInMillis()+(1000*24*60*60));
datePickerDialog.show();
我的DATEPICKER风格。根据需要定制...
<style name="datepicker" parent="@android:style/Theme.Holo.Light.Dialog">
<!---TODO-->
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:focusedMonthDateColor">@android:color/holo_red_dark</item>
</style>
<强>更新强> 将样式更新为github库dtpicker
<style name="datepicker" parent="@android:style/Theme.DeviceDefault.Light.Dialog">
<!---TODO-->
<item name="android:focusedMonthDateColor">@android:color/holo_red_dark</item>
</style>