【问题标题】:CalendarView getDate method returns current date, not selected date... What am I doing wrong?CalendarView getDate 方法返回当前日期,而不是选定的日期......我做错了什么?
【发布时间】:2019-08-05 14:55:31
【问题描述】:

我的 calendarView 无法返回所选日期,而是返回一些始终指向今天的默认值。

我当然要更改日历中选择的日期,而且它确实显示为已更改。我尝试在调试模式下检查视图,但没有找到任何东西。

我是在模拟器上运行的,而不是在真机上...我应该修改一些设置吗?我错过了什么重要的东西吗?因为我没有得到选定的日期,而是当前日期,这真的很令人困惑。

<CalendarView
                            android:id="@+id/view_calendar_create_event_date"
                            android:layout_width="wrap_content"
                            android:layout_height="0dp"
                            android:layout_weight="1" />

这是从事件监听器调用的

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());
    }

这就是我设置事件监听器的方式

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();
            }
        });

【问题讨论】:

    标签: android calendarview


    【解决方案1】:

    你需要实现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
        }
     });
    

    【讨论】:

    • 就是这样......你知道为什么日历视图会这样吗?这有点出乎意料(至少可以说)。此外,它没有记录。我发现了这个:developer.android.com/reference/android/widget/…,它明确表示它获得了选定的日期,而不是当前日期....你知道这是一个错误还是我遗漏了什么?
    • 很高兴我提供了帮助,这不是错误。它正在按照文档中的说明工作。默认情况下,选定的日期是今天的日期。当您更改日期时,实际上您并没有选择任何新日期,而不是更改所选日期,您可以在 setOnDateChangeListener 上获取该操作。
    猜你喜欢
    • 2015-10-14
    • 1970-01-01
    • 2015-02-17
    • 1970-01-01
    • 1970-01-01
    • 2014-10-05
    • 2016-08-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多