如何知道CalendarView当前显示的月份和年份?

时间:2020-05-14 18:04:10

标签: java android android-calendar calendarview

我正在开发一个基于MaterialCalendarView(prolificinteractive)的应用程序,我正在用可绘制对象等装饰日期。我想知道用户当前可以看到哪个月份和年份,因此我只能将装饰器应用于当前可见对象一个月而不是整个月,因为它会毫无理由地降低应用程序运行速度。注意:我不是在说当前选择的日期。

1 个答案:

答案 0 :(得分:1)

已经有一个OnMonthChangedListener,但是它甚至没有月份参数!如果您不介意反思,可以这样做:

calview.setOnMonthChangedListener((widget, date) -> {
    try {
        Field currentMonthField = MaterialCalendarView.class.getDeclaredField("currentMonth");
        currentMonthField.setAccessible(true);
        int currentMonth = ((CalendarDay) currentMonthField.get(widget)).getMonth();
        // Do something, currentMonth is between 1 and 12.

    } catch (NoSuchFieldException | IllegalAccessException e) {
        // Failed to get field value, maybe library was changed.
    }
});

或者更好的是,打开并发出或发出拉取请求,以使该字段在调用侦听器时传递其值时公开。

相关问题