[English]

Calendar(日历) (lv_calendar)

Overview(概述)

显示原文

The Calendar object is a classic calendar which can: - show the days of any month in a 7x7 matrix - Show the name of the days - highlight the current day (today) - highlight any user-defined dates

The Calendar is added to the default group (if it is set). Calendar is an editable object which allow selecting and clicking the dates with encoder navigation too.

To make the Calendar flexible, by default it doesn't show the current year or month. Instead, there are optional "headers" that can be attached to the calendar.


Calendar 对象是一个经典日历,它可以:

  • 通过一个7x7矩阵显示任何月份

  • 显示日期名称

  • 突出显示当前日期(今天)

  • 突出显示任何用户定义的日期

日历默认会添加到默认组(如果已设置)。日历控件可以通过编码器的导航模式选中,之后可以通过编辑模式选择指定的日期。

为了使日历灵活,默认情况下它不显示当前的年份或月份。但是可以通过可选的 “标头(headers)” 附加到日历的顶部,这样就可以显示当前的年份及月份,并且可以切换到其他年份或月份。

Parts and Styles(部分和样式)

显示原文

The calendar object uses the Button matrix object under the hood to arrange the days into a matrix.

  • LV_PART_MAIN The background of the calendar. Uses all the background related style properties.

  • LV_PART_ITEMS Refers to the dates and day names. Button matrix control flags are set to differentiate the buttons and a custom drawer event is added modify the properties of the buttons as follows:

    • day names have no border, no background and drawn with a gray color

    • days of the previous and next month have LV_BUTTONMATRIX_CTRL_DISABLED flag

    • today has a thicker border with the theme's primary color - highlighted days have some opacity with the theme's primary color.


calendar 对象的底层是通过按钮矩阵对象将日期排列到(7x7)矩阵中。

  • LV_PART_MAIN 日历的背景。使用所有与背景相关的样式属性。

  • LV_PART_ITEMS 指日期和日期名称。设置按钮矩阵控制标志来区分不同的按钮(日期),并添加自定义绘制(drawer)事件,可参考如下方式修改按钮的属性:

    • 日期名称没有边框,没有背景,用灰色绘制

    • 矩阵中上个月和下个月的天数有 LV_BUTTONMATRIX_CTRL_DISABLED 标志

    • 今天(你指定的日期)有较厚的边框(使用主题的主色)

    • 突出显示的日期有一些不透明度(使用主题的主色)。

Usage(用法)

显示原文

Some functions use the lv_calendar_date_t type which is a structure with year, month and day fields.


某些函数使用 lv_calendar_date_t 结构体类型,这个结构体有 year, monthday 成员。

Current date(当前日期)

显示原文

To set the current date (today), use the lv_calendar_set_today_date(calendar, year, month, day) function. month needs to be in 1..12 range and day in 1..31 range.


要设置当前日期(今天),请使用 lv_calendar_set_today_date(calendar, year, month, day) 函数。 month(月) 的范围:1..12 ; day(日) 的范围:1..31。

Shown date(显示日期)

显示原文

To set the shown date, use lv_calendar_set_shown_date(calendar, year, month)


设置想展示的日期(年月),请使用 lv_calendar_set_shown_date(calendar, year, month) 函数。

Highlighted days(突出显示的日期)

显示原文

The list of highlighted dates should be stored in a lv_calendar_date_t array loaded by lv_calendar_set_highlighted_dates(calendar, highlighted_dates, date_num). Only the array's pointer will be saved so the array should be a static or global variable.


可以指定多个想要突出显示的日期,突出显示的日期列表应存储在 lv_calendar_date_t 类型的数组中,其通过 lv_calendar_set_highlighted_dates(calendar, highlighted_dates, date_num) 函数加载,该函数只会保存数组的指针,所以该数组应该是静态的或全局变量。

Name of the days(日期名称)

显示原文

The name of the days can be adjusted with lv_calendar_set_day_names(calendar, day_names) where day_names looks like const char * day_names[7] = {"Su", "Mo", ...}; Only the pointer of the day names is saved so the elements should be static, global or constant variables.


可以使用函数 lv_calendar_set_day_names(calendar, day_names) 调整日期的名称(默认是星期一至星期天的英文缩写),其中参数 day_names 可以是类似这样的数组 const char * day_names[7] = {"Su", "Mo", ...}; 只有保存日期名称的指针,因此元素应该是静态的,全局变量或常量变量。

Custom year list(自定义年份列表)

显示原文

Sets a custom year list with lv_calendar_header_dropdown_set_year_list(calendar, years_list) where years_list is a pointer to the custom years list. It can be a constant string like static const char * years = "2023\n2022\n2021\n2020\n2019";, or can be generated dynamically into a buffer as well.


使用 lv_calendar_header_dropdown_set_year_list(calendar, years_list) 函数设置自定义年份列表,其中参数 years_list 是指向自定义年份列表的指针。它可以是一个常量字符串比如 static const char * years = "2023\n2022\n2021\n2020\n2019";,或者也可以动态生成到缓冲区中。

Chinese calendar(中国历法)

显示原文

The Chinese calendar is a traditional cultural tool that integrates elements such as the lunar calendar, solar terms, and traditional festivals. It is widely used in Chinese social life, helping people understand the dates of the lunar calendar, arrange festival activities, and inherit the excellent traditional culture of the Chinese nation. Whether in families, businesses, or education, the Chinese calendar plays an irreplaceable role, enabling people to better understand and appreciate the charm of Chinese traditional culture.

If you want to use the Chinese calendar, please use lv_calendar_set_chinese_mode(calendar, true) to enable it.


中国历法是一种融合了农历、节气和传统节日等元素的传统文化工具。它广泛应用于中国社会生活中,帮助人们了解农历日期,安排节日活动,继承中华民族优秀的传统文化。无论是在家庭、商业还是教育领域,中国历法都发挥着不可替代的作用,使人们能够更好地理解和欣赏中国传统文化的魅力。

如果您想使用中国历法,请使用函数 lv_calendar_set_chinese_mode(calendar, true) 启用它。

Headers(头部)

显示原文

From v8.1 the header is added directly into the Calendar widget and the API of the headers has been changed.


从 v8.1 开始,标题直接添加到日历小部件中,并且 标头的 API 已更改。

Arrow buttons(箭头按钮)

显示原文

lv_calendar_header_arrow_create(calendar) creates a header that contains a left and right arrow on the sides and a text with the current year and month between them.


lv_calendar_header_arrow_create(calendar) 创建一个标头,该标头两侧包含一个左右箭头和一个带有当前他们之间的年和月

Events(事件)

显示原文

Learn more about Events(事件).


  • LV_EVENT_VALUE_CHANGED 如果单击日期,则发送该事件,通过调用函数 lv_calendar_get_pressed_date(calendar, &date)date 设置为当前按下的日期。如果存在,则返回 LV_RES_OK 有效的按下日期,否则 LV_RES_INVALID

详细了解更多 Events(事件)

Keys(按键)

显示原文
  • LV_KEY_RIGHT/UP/LEFT/RIGHT To navigate among the buttons to dates

  • LV_KEY_ENTER To press/release the selected date

Learn more about Keys(按键).


  • LV_KEY_RIGHT/UP/LEFT/RIGHT 在按钮之间导航到日期

  • LV_KEY_ENTER 按下/松开所选日期

详细了解更多 Keys(按键)

Example

[English]

Calendar with header

#include "../../lv_examples.h"
#if LV_USE_CALENDAR && LV_BUILD_EXAMPLES

static void event_handler(lv_event_t * e)
{
    lv_event_code_t code = lv_event_get_code(e);
    lv_obj_t * obj = lv_event_get_current_target(e);

    if(code == LV_EVENT_VALUE_CHANGED) {
        lv_calendar_date_t date;
        if(lv_calendar_get_pressed_date(obj, &date)) {
            LV_LOG_USER("Clicked date: %02d.%02d.%d", date.day, date.month, date.year);
        }
    }
}

void lv_example_calendar_1(void)
{
    lv_obj_t  * calendar = lv_calendar_create(lv_screen_active());
    lv_obj_set_size(calendar, 185, 230);
    lv_obj_align(calendar, LV_ALIGN_CENTER, 0, 27);
    lv_obj_add_event_cb(calendar, event_handler, LV_EVENT_ALL, NULL);

    lv_calendar_set_today_date(calendar, 2021, 02, 23);
    lv_calendar_set_showed_date(calendar, 2021, 02);

    /*Highlight a few days*/
    static lv_calendar_date_t highlighted_days[3];       /*Only its pointer will be saved so should be static*/
    highlighted_days[0].year = 2021;
    highlighted_days[0].month = 02;
    highlighted_days[0].day = 6;

    highlighted_days[1].year = 2021;
    highlighted_days[1].month = 02;
    highlighted_days[1].day = 11;

    highlighted_days[2].year = 2022;
    highlighted_days[2].month = 02;
    highlighted_days[2].day = 22;

    lv_calendar_set_highlighted_dates(calendar, highlighted_days, 3);

#if LV_USE_CALENDAR_HEADER_DROPDOWN
    lv_calendar_header_dropdown_create(calendar);
#elif LV_USE_CALENDAR_HEADER_ARROW
    lv_calendar_header_arrow_create(calendar);
#endif
}

#endif

Chinese calendar

#include "../../lv_examples.h"
#if LV_USE_CALENDAR && LV_USE_CALENDAR_CHINESE && LV_BUILD_EXAMPLES

void lv_example_calendar_2(void)
{
    lv_obj_t  * calendar = lv_calendar_create(lv_screen_active());
    lv_obj_set_size(calendar, 300, 300);
    lv_obj_align(calendar, LV_ALIGN_TOP_MID, 0, 0);

    lv_calendar_set_today_date(calendar, 2024, 03, 22);
    lv_calendar_set_showed_date(calendar, 2024, 03);

#if LV_USE_CALENDAR_HEADER_DROPDOWN
    lv_calendar_header_dropdown_create(calendar);
#elif LV_USE_CALENDAR_HEADER_ARROW
    lv_calendar_header_arrow_create(calendar);
#endif

    lv_calendar_set_chinese_mode(calendar, true);
    lv_obj_set_style_text_font(calendar, &lv_font_simsun_14_cjk, LV_PART_MAIN);
}

#else

void lv_example_calendar_2(void)
{
    lv_obj_t * label = lv_label_create(lv_screen_active());
    lv_label_set_text(label, "chinese calendar is not enabled");
    lv_obj_center(label);
}

#endif

API

lv_calendar_header_arrow.h

lv_calendar_header_dropdown.h

lv_types.h

lv_calendar.h

lv_calendar_chinese.h