Android: CalendarView OnDateChangeLIstener

前端 未结 5 1037
醉话见心
醉话见心 2020-12-03 21:08

I\'ve already implemented this listener in order for me to display something when a certain date is clicked, but the problem is that when i scroll the CalendarView down, it

5条回答
  •  南笙
    南笙 (楼主)
    2020-12-03 21:45

    I just had the same problem and found a work-around for it.

    create "Long date" variable and when you start your calender window save the current date in this variable.

    Long date;
    cv = (CalendarView)findViewById(R.id.calendarView1);
    date = cv.getDate();
    

    now in the listener just check if the new date is same as the calendar:

    cv.setOnDateChangeListener(new OnDateChangeListener(){
            public void onSelectedDayChange(CalendarView view, int year, int month, int dayOfMonth) {
                if(cv.getDate() != date){
                    date = cv.getDate();
                    Toast.makeText(view.getContext(), "Year=" + year + " Month=" + month + " Day=" + dayOfMonth, Toast.LENGTH_LONG).show();
                }               
            }
        });
    

    it worked for me :)

提交回复
热议问题