android 工作室中的日期选择器验证
Date Picker Validation in android studio
当今天的日期比上个月的 16-31 日期介于 1-15 之间时启用。
当今天日期在 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();
你的 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();
}
当今天的日期比上个月的 16-31 日期介于 1-15 之间时启用。 当今天日期在 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();
你的 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();
}