Android: CalendarView OnDateChangeLIstener

前端 未结 5 916
醉话见心
醉话见心 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 :)

    0 讨论(0)
  • 2020-12-03 21:58

    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);
    }
    
    0 讨论(0)
  • 2020-12-03 21:59

    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..
    
    }
    
    0 讨论(0)
  • 2020-12-03 21:59

    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.

    0 讨论(0)
  • 2020-12-03 22:00

    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.

    0 讨论(0)
提交回复
热议问题