当今天的日期在1-15之间,比上个月16-31日期启用。 当今日期在16-31之间,比当前月份1-15日期启用。我的代码请解决我的问题:
private void DateDialog() {
final DatePickerDialog dpDialog = new DatePickerDialog(this,android.app.AlertDialog.THEME_DEVICE_DEFAULT_DARK,new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker datePicker, int day, int month, int year) {
cal = Calendar.getInstance();
cal.setTimeInMillis(System.currentTimeMillis());
//int curyear = cal.get(Calendar.YEAR);
cal.set(day, month, year);
}
}, day, month, year);
if ((day < 15)) {
int month=cal.get(Calendar.MONTH);
Log.e("Month", String.valueOf(month));
int days = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
Log.e("days", String.valueOf(days));
int lastday = days-day;
Log.e("Lastday", String.valueOf(lastday));
dpDialog.getDatePicker().setMinDate((month-1)+System.currentTimeMillis()-(1000 * 60 * 60 * 24*(days-day)));
long now = System.currentTimeMillis() - 1000;
// dp_time.setMinDate(now);
dpDialog.getDatePicker().setMaxDate((month-1)+now +(1000 * 60 * 60 * 24 *15));
}
else if ((day > 15)) {
int days = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
Log.e("days", String.valueOf(days));
int lastday = days - 15;
Log.e("lastday", String.valueOf(lastday));
int month=cal.get(Calendar.MONTH);
Log.e("Month", String.valueOf(month));
dpDialog.getDatePicker().setMinDate((1000*60*60*24*(day+1)));
long now = System.currentTimeMillis() - 1000;
// dp_time.setMinDate(now);
dpDialog.getDatePicker().setMaxDate(now +(1000 * 60 * 60 * 24 *lastday));
}
dpDialog.show();
答案 0 :(得分:0)
你的DatePickerDialog
构造函数值不正确,你过了一天而不是一年,反之亦然,条件也几乎没有问题。
这里有更新的代码。只需用这个代码替换你的代码。
private void DateDialog() {
final Calendar calendar = Calendar.getInstance();
final DatePickerDialog dpDialog = new DatePickerDialog(this, android.app.AlertDialog.THEME_DEVICE_DEFAULT_DARK, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker datePicker, int day, int month, int year) {
cal.set(day, month, year);
// cal.set(day, month, year);
}
}, year, month, day);
if (day > 15) {
calendar.set(Calendar.DAY_OF_MONTH, 1);
dpDialog.getDatePicker().setMinDate(calendar.getTimeInMillis());
dpDialog.getDatePicker().setMaxDate(System.currentTimeMillis());
} else {
calendar.add(Calendar.MONTH, -1);
calendar.set(Calendar.DAY_OF_MONTH, 16);
dpDialog.getDatePicker().setMinDate(calendar.getTimeInMillis());
dpDialog.getDatePicker().setMaxDate(System.currentTimeMillis());
}
dpDialog.show();
}