简体   繁体   中英

Android Calendarview set focused date ( Previous and Next month)

I'm using calendarview library and I have a "Previous" and "Next" buttons. Can you please help me move the focus to the "Previous" month and "Next" month?

the codes below does not work for me..

 // format date and display on screen
        final Calendar dat = Calendar.getInstance();
        dat.set(Calendar.YEAR, year);
        dat.set(Calendar.MONTH, month);
        dat.set(Calendar.DAY_OF_MONTH, day);

I appreciate the help.

I am not sure if you are setting the new date in the CalendarView .

The code that you want would look something like this (for getting the previous month)

Calendar cal = Calendar.getInstance(); // Get today
cal.add(Calendar.MONTH, -1); // get previous month by substracting one to the current one
long previousMonth = cal.getTimeInMillis(); // get the milliseconds of previous month
calendarView.setDate(previousMonth); // set in the CalendarView that you want to see the previous month

The code for moving forward is just a matter of changing the line

 cal.add(Calendar.MONTH, -1); // get previous month

to

 cal.add(Calendar.MONTH, 1); // get following month

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM