CalendarView getDate method returns current date, not selected date... What am I doing wrong?

vla*_*ean 2 android calendarview

My calendarView fails to return the selected date, instead returning some default that always points to today.

I am of course changing the date selected in my calendar, and it indeed displays as having changed. I tried inspecting the view in debug mode, but didn't find anything.

I am running this in a simulator, not on a real phone... Should I modify some settings? Am I missing something important? Because it really is confusing that I'm not getting the selected date, but the current one.

<CalendarView
                            android:id="@+id/view_calendar_create_event_date"
                            android:layout_width="wrap_content"
                            android:layout_height="0dp"
                            android:layout_weight="1" />
Run Code Online (Sandbox Code Playgroud)

This is called from the event listener

protected void createEvent(View view){
        TextView eventNameView = (TextView) this.findViewById(R.id.createEventNameInput);
        String eventName = eventNameView.getEditableText().toString();

        CalendarView eventOccursOnView = (CalendarView) this.findViewById(R.id.view_calendar_create_event_date);
        long eventOccursOn = eventOccursOnView.getDate();
        Date temporary = new Date(eventOccursOn);

        Event newEvent = new Event(eventName, "", 0, 0, eventOccursOn);
        newEvent.save(view.getContext());
    }
Run Code Online (Sandbox Code Playgroud)

and this is how I'm setting my event listener

saveButton.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view) {
                // Create the event
                EventDetailsActivity.this.createEvent(view);

                // Notify the user
                Snackbar.make(view, "Successfully created a new event!", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();

                // Return to the previous activity
                finish();
            }
        });
Run Code Online (Sandbox Code Playgroud)

Mit*_*vro 5

您需要实施 setOnDateChangeListener

long eventOccursOn;

eventOccursOnView.setOnDateChangeListener(new OnDateChangeListener() {
    //show the selected date as a toast
    @Override
    public void onSelectedDayChange(CalendarView view, int year, int month, int day) {
        Toast.makeText(getApplicationContext(), day + "/" + month + "/" + year, Toast.LENGTH_LONG).show();
        Calendar c = Calendar.getInstance();
        c.set(year, month, day);
        eventOccursOn = c.getTimeInMillis(); //this is what you want to use later
    }
 });
Run Code Online (Sandbox Code Playgroud)

  • 就是这样...您知道日历视图为何会如此显示吗?这有点意外(至少可以说)。此外,它没有记录。我发现了这一点:https://developer.android.com/reference/android/widget/CalendarView.html#getDate()明确表示它获取了选定的日期,而不是当前日期。一个错误或我缺少什么? (3认同)
  • 很高兴我提供了帮助,这不是一个错误。它正在按照文档中提到的那样工作。默认情况下,所选日期是今天的日期。当您实际更改日期时,您并没有选择任何新日期,而不是更改所选日期,您可以在 setOnDateChangeListener 上获取该操作。 (2认同)