日历 (Calendar)

![image.png](https://cos.easydoc.net/71161452/files/ks2owvnt.png) LVGL 提供了一个用来选择和显示当前日期的日历控件。 ## 示例代码 ```lua -- 高亮显示的日期 highlightDate = lvgl.calendar_date_t() -- 日历点击的回调函数 -- 将点击日期设置高亮 function event_handler(obj, event) if event == lvgl.EVENT_VALUE_CHANGED then date = lvgl.calendar_get_pressed_date(obj) if date then print(string.format("Clicked date: %02d.%02d.%d\n", date.day, date.month, date.year)) highlightDate.year = date.year highlightDate.month = date.month highlightDate.day = date.day lvgl.calendar_set_highlighted_dates(obj, highlightDate, 1) end end end -- 创建日历 calendar = lvgl.calendar_create(lvgl.scr_act(), nil) lvgl.obj_set_size(calendar, 235, 235) lvgl.obj_align(calendar, nil, lvgl.ALIGN_CENTER, 0, 0) lvgl.obj_set_event_cb(calendar, event_handler) -- 设置今天日期 today = lvgl.calendar_date_t() today.year = 2018 today.month = 10 today.day = 23 lvgl.calendar_set_today_date(calendar, today) lvgl.calendar_set_showed_date(calendar, today) ``` ## 创建 通过 `lvgl.calendar_create` 函数可以获取一个日历控件。 ```lua calendar = lvgl.calendar_create(lvgl.scr_act(), nil) ``` 要知道日历控件是个大家伙,虽然可以对日历控件设置大小,但是如果如果尺寸显示过小,显示效果会严重折扣。毕竟这样的控件不会像那些简单的矢量图形一样可以自动调整。 ```lua lvgl.obj_set_size(calendar, 128, 160) ``` 设置日历大小 `128*160` 显示效果就是这样。所以屏幕分辨率不是很高,不建议使用日历控件。 ![image.png](https://cos.easydoc.net/71161452/files/ks1roq6d.png) ## 使用 日历控件当然是用在显示日期的地方,一个日历控件就能当成是一个应用了。也可以辅助用户选择日期,让用户输入一个日期还是比较繁琐的,格式也不好确定,而且用户很有可能会输入一个不存在的日期 `2021年02月29日`。通过日历控件可以很好的辅助用户选择日期。 ## 日期 日期是通过 `lvgl.calendar_date_t()` 函数创建的,这是个用户自定义数据,注意不要用 `talbe` 创建日期,两者还是有区别的。 ```lua today = lvgl.calendar_date_t() today.year = 2018 today.month = 8 today.day = 8 ``` ## 选择/设置日期 日历控件有三个常用日期,今天的日期,高亮日期,当前显示的日期。 ``` today = lvgl.calendar_date_t() today.year = 2018 today.month = 10 today.day = 23 lvgl.calendar_set_today_date(calendar, today) -- 设置当前日期 show = lvgl.calendar_date_t() show.year = 2018 show.month = 9 show.day = 23 lvgl.calendar_set_showed_date(calendar, show) -- 设置显示日期 light = lvgl.calendar_date_t() light.year = 2018 light.month = 10 light.day = 23 lvgl.calendar_set_highlighted_dates(calendar, light, 1) -- 设置高亮日期 ``` 来看下这三个日期的区别: ![image.png](https://cos.easydoc.net/71161452/files/ks2oxdbg.png) `2018年10月11日`是高亮显示的效果。 `2018年10月23日`是当前日期的显示效果。 但是控件默认显示是在9月,因为显示的日期是`2018年09月23日`。 ![jdfw.gif](https://cos.easydoc.net/71161452/files/ks2ozkm5.gif) ## 事件 除了对象的通用事件以外,日历控件可以通过 `lvgl.EVENT_VALUE_CHANGED` 事件获取当前用户点击的日期。 ```lua function event_handler(obj, event) if event == lvgl.EVENT_VALUE_CHANGED then date = lvgl.calendar_get_pressed_date(obj) if date then print(string.format("Clicked date: %02d.%02d.%d\n", date.day, date.month, date.year)) end end end lvgl.obj_set_event_cb(calendar, event_handler) ``` ![jdfw2.gif](https://cos.easydoc.net/71161452/files/ks2ppthq.gif) ## API ### lvgl.calendar_create |调用|lvgl.calendar_create(par, copy)| |-|-| |功能|创建日历对象| |返回|指向创建的日历的指针| |参数|| |par|指向对象的指针, 它将是新日历的父对象| |copy|指向日历对象的指针, 如果不为 nil, 则将从其复制新对象| ### lvgl.calendar_set_today_date |调用|lvgl.calendar_set_today_date(calendar, today)| |-|-| |功能|设定今天的日期| |参数|| |calendar|指向日历对象的指针| |today|指向包含今天的日期的变量的指针| ### lvgl.calendar_set_showed_date |调用|lvgl.calendar_set_showed_date(calendar, showed)| |-|-| |功能|设置当前显示| |参数|| |calendar|指向日历对象的指针| |showed|指向包含要显示日期的变量的指针。| ### lvgl.calendar_set_highlighted_dates |调用|lvgl.calendar_set_highlighted_dates(calendar, light, num)| |-|-| |功能|设置高亮显示日期| |参数|| |calendar|指向日历对象的指针| |light|指向包含要显示日期的变量的指针。| |num|高亮日期个数。| ### lvgl.calendar_get_today_date |调用|lvgl.calendar_get_today_date(calendar)| |-|-| |功能|获取今天的日期| |返回|指向包含今天的日期的变量的指针。| |参数|| |calendar|指向日历对象的指针| ### lvgl.calendar_get_showed_date |调用|lvgl.calendar_get_showed_date(calendar)| |-|-| |功能|获取当前显示| |返回|包含日期的变量的指针正在显示。| |参数|| |calendar|指向日历对象的指针| ### lvgl.calendar_get_pressed_date |调用|lvgl.calendar_get_pressed_date(calendar)| |-|-| |功能|获取按下日期。| |返回|指向包含按下日期的变量的指针。如果当前未按下,显示 nil。| |参数|| |calendar|指向日历对象的指针 [参考链接(C语言)](https://docs.lvgl.io/7.11/widgets/calendar.html)