【问题标题】:I'm creating a CalendarView, but it takes the previous month instead of the current month?我正在创建一个 CalendarView,但它需要上个月而不是当月?
【发布时间】:2021-02-09 18:07:56
【问题描述】:

我正在创建一个 CalendarView,但它需要上个月而不是当前月份。 帮帮我!

    <CalendarView
    android:id="@+id/calendarView"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/radioGup" />

calendarView.setOnDateChangeListener { 查看, 年, 月, dayOfMonth ->

date = dayOfMonth.toString() + "-" + month.toString() + "-" +year.toString() Toast.makeText(activity,date,Toast.LENGTH_SHORT).show() 适配器(日期) }

说月份 = 1

【问题讨论】:

  • 你们是如何实现的?请出示代码
  • 不,我不知道,都是 kotlin

标签: java android kotlin


【解决方案1】:
/**
         * Called upon change of the selected day.
         *
         * @param view The view associated with this listener.
         * @param year The year that was set.
         * @param month The month that was set [0-11].
         * @param dayOfMonth The day of the month that was set.
         */
        void onSelectedDayChange(@NonNull CalendarView view, int year, int month, int dayOfMonth);

这将返回从 0 到 11 的月份值,即。 0 表示 1 月 11 日表示 12 月。 所以我们必须在月值中加 1

【讨论】:

  • 那么如果月亮 = 11,我们加 1,那不是 12 吗?
  • 因为我们将 1 月视为第一个月,将 11 月视为第 11 个月。因此,12 月肯定是第 12 个月。现在这个 CalendarView 返回从 0 到 11 范围内的月份值。所以要得到实际的月份值,我们必须加 1。
猜你喜欢
  • 2013-10-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-23
相关资源
最近更新 更多