【问题标题】:Android CalendarView showing wrong month on scroll when we set max date当我们设置最大日期时,Android CalendarView 在滚动时显示错误的月份
【发布时间】:2016-12-28 21:02:16
【问题描述】:

我为 calendarview 添加了 setMinDate(Dec 1, 2015) 和 setMaxDate(current month last day),当从最小日期移动到最大日期时,它显示上个月(默认情况下它显示 12 月,当我滚动到从最大日期开始的最小日期,然后从最小日期到最大日期,最后显示 11 月)。以及如何从日历中删除不集中的月份,任何人都可以建议我

这是我的代码

public void showCalendar() {

        AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(ctx);
        LayoutInflater inflater = ((Activity) ctx).getLayoutInflater();
        final View dialogView = inflater.inflate(R.layout.calendar_view_layout,
                null);

        CalendarView calendar = (CalendarView) dialogView
                .findViewById(R.id.calendarView);

        Button cancelBtn = (Button) dialogView.findViewById(R.id.cancel);
        Button okBtn = (Button) dialogView.findViewById(R.id.ok);

        // sets whether to show the week number.
        calendar.setShowWeekNumber(false);

        // sets the first day of week according to Calendar.
        // here we set Monday as the first day of the Calendar
        calendar.setFirstDayOfWeek(2);
        calendar.setBackgroundColor(Color.WHITE);
        calendar.setFocusedMonthDateColor(Color.BLACK);




        /* calendar.setMinDate(); */
        calendar.setMaxDate(System.currentTimeMillis());

        // The background color for the selected week.
        calendar.setSelectedWeekBackgroundColor(Color.TRANSPARENT);

        // sets the color for the dates of an unfocused month.
        calendar.setUnfocusedMonthDateColor(Color.BLACK);

        // sets the color for the separator line between weeks.
        calendar.setWeekSeparatorLineColor(getResources().getColor(
                R.color.transparent));

        // sets the color for the vertical bar shown at the beginning and at the
        // end of the selected date.
        // calendar.setSelectedDateVerticalBar(Color.GREEN);

        // sets the listener to be notified upon selected date change.
        calendar.setOnDateChangeListener(new OnDateChangeListener() {
            // show the selected date as a toast
            @Override
            public void onSelectedDayChange(CalendarView view, int year,
                    int month, int day) {

                currentMonth = month;
                currentDay = day;
                currentYear = year;

                /*
                 * Toast.makeText(getActivity(), day + "/" + (month + 1) + "/" +
                 * year, Toast.LENGTH_LONG) .show();
                 */
            }
        });

        getCurrentMonth(calendar);

        dialogBuilder.setView(dialogView);
        final AlertDialog b = dialogBuilder.create();
        b.getWindow().setBackgroundDrawable(
                new ColorDrawable(android.graphics.Color.TRANSPARENT));
        b.show();

    }

    void getCurrentMonth(CalendarView calendarView) {

        Date today = new Date();

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(today);

        calendar.add(Calendar.MONTH, 1);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.add(Calendar.DATE, -1);

        Date lastDayOfMonth = calendar.getTime();

        DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        long maxDate = lastDayOfMonth.getTime();
        calendarView.setMaxDate(maxDate);
        String[] monthLastDay = sdf.format(lastDayOfMonth).split("-");

        int year = Integer.parseInt(monthLastDay[0]) - 1;

        String lastYearFirstDate = year + "-" + monthLastDay[1] + "-" + 01;

        try {
            long minDate = sdf.parse(lastYearFirstDate).getTime();
            calendarView.setMinDate(minDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }

【问题讨论】:

    标签: android calendarview


    【解决方案1】:

    看看here。月份从 0 开始,因此,0 = 一月,11 = 十二月。 第二个问题不清楚,请解释清楚,举几个例子就好了

    【讨论】:

    • 刚刚添加了showWeekCount(5),现在显示正确
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-08-09
    • 2016-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多