试图解决这个问题一整天,但无法解决问题。
我想要的是将日历视图的最小值和最大值设置为当前月份和全年的日期。
因此用户无法滚动到上个月或下个月。
到目前为止我尝试的是:
calendar = Calendar.getInstance();
int currentYear = calendar.get(Calendar.YEAR);
int currentMonth = calendar.get(Calendar.MONTH) + 1;
int currentDay = calendar.get(Calendar.DATE);
int minMonthCalendarView = calendar.getActualMaximum(Calendar.DAY_OF_MONTH) + 1;
int maxMonthCalendarView = calendar.getActualMaximum(Calendar.DAY_OF_MONTH) + 2;
technicianDateHint.setText(getResources().getString(R.string.Your) + " " + String.valueOf(currentDay) + "-" +
String.valueOf(currentMonth) + "-" + String.valueOf(currentYear));
try {
date = DDMMMYYYY.parse(item.getDueDate());
item.setDueDate(DDMMYYY.format(date));
} catch (ParseException e1) {
e1.printStackTrace();
}
userDate.setText(item.getDueDate());
technicianDate.setShowWeekNumber(false);
technicianDate.setMinDate(minMonthCalendarView);
technicianDate.setMaxDate(maxMonthCalendarView);
technicianDate.setUnfocusedMonthDateColor(Color.TRANSPARENT);
technicianDate.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
@Override
public void onSelectedDayChange(CalendarView calendarView, int year, int month, int day) {
technicianDateHint.setText(day + "-" + month + "-" + year);
}
});
int minMonthCalendarView = calendar.getActualMaximum(Calendar.DAY_OF_MONTH)+ 1; int maxMonthCalendarView = calendar.getActualMaximum(Calendar.DAY_OF_MONTH)+ 2;
当使用minDate设置而没有maxDate时,它可以正常工作。由于用户无法滚动到上个月,但可以滚动到下个月。
有没有办法将setMinDate和setMaxDate设置为当月?
答案 0 :(得分:2)
试试这个:
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.DATE, calendar.getActualMaximum(Calendar.DATE));
calendar.set(Calendar.HOUR_OF_DAY, 23);//not sure this is needed
long endOfMonth = calendar.getTimeInMillis();
//may need to reinitialize calendar, not sure
calendar = Calendar.getInstance();
calendar.set(Calendar.DATE, 1);
calendar.set(Calendar.HOUR_OF_DAY, 0);
long startOfMonth = calendar.getTimeInMillis();
calendarView.setMaxDate(endOfMonth);
calendarView.setMinDate(startOfMonth);