Android: CalendarView OnDateChangeLIstener

最后都变了- 提交于 2019-11-27 15:29:27

When you are scrolling the calendar onSelectedDayChange method behaves like you click on different date but that won't change current date settings. So HFDO5 was right, you just need to save current date when you create your calendar: Long date = calendar.getDate();

and check it in onSelectedDayChange.

if(callendar.getDate() != date){
date = calendar.getDate(); //new current date
//date is changed on real click...do things..

}

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 :)

Finally I decided I needed to display an "OK" button below the CalendarView so the date can be chosen and the calendar dismissed.

Because when you click the current date nothing happens, and when you scroll you get calls to your OnDateChangeListener.

Example of calendar layout with black mask background you can click to dismiss:

<LinearLayout
    android:id="@+id/calendar_layout"
    android:onClick="dismissCalendar"
    android:visibility="gone"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="20dp"
    android:background="@color/black_mask">

    <CalendarView
        android:id="@+id/calendar"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="@color/white"
        android:showWeekNumber="false"
        android:firstDayOfWeek="2"
        />

    <TextView
        android:onClick="selectDate"
        android:text="@string/select_date"
        android:background="@color/colorPrimary"
        style="@style/settings_button"/>

</LinearLayout>

Then sample code:

private void openCalendar(Date date) {
    calendar.setDate(date.getTime());
    calendarLayout.setVisibility(View.VISIBLE);
}

public void selectDate(View view) {
    Date date = new Date(calendar.getDate());
    // do whatever you need with date
    dismissCalendar(view);
}

public void dismissCalendar(View view) {
    calendarLayout.setVisibility(View.INVISIBLE);
}

My way around this was using calendar from HoloEverywhere (available source code for customization), commenting out the code causing this to be invoked and invoking it in onDateTapped method, that does the expected thing. And it works on pre ICS devices too.

Maybe my solution to my previous problem will give you some idea. When I pick a date from the calendar view, a google map is displayed showing the route that I walked for that date. Because the latitude and longitude are retrieved from database, so I query the database for that date and if there's no record the app pops up a toast message. Otherwise it shows a google map with the route.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!