LVGL 日历组件 calendar
添加到默认组中
- lvgl的 calendar 组件会默认添加到旋钮按键组中,需要手动将其删除,否则会聚焦到不可见的控件上。
lv_obj_set_style_pad_all(calendar, 8, LV_STATE_DEFAULT);
lv_group_remove_obj(calendar);
修改时间范围
- 年时间范围需要手动修改源码,下面就是将年信息修改到2023-2099年
#include "lv_calendar_header_dropdown.h"
#if LV_USE_CALENDAR_HEADER_DROPDOWN
#include "lv_calendar.h"
#include "../../../widgets/lv_dropdown.h"
#include "../../layouts/flex/lv_flex.h"
static void my_constructor(const lv_obj_class_t* class_p, lv_obj_t* obj);
static void year_event_cb(lv_event_t* e);
static void month_event_cb(lv_event_t* e);
static void value_changed_event_cb(lv_event_t* e);
const lv_obj_class_t lv_calendar_header_dropdown_class = {
.base_class = &lv_obj_class,
.width_def = LV_PCT(100),
.height_def = LV_SIZE_CONTENT,
.constructor_cb = my_constructor
};
static const char* month_list = "01\n02\n03\n04\n05\n06\n07\n08\n09\n10\n11\n12";
static const char* year_list = {
"2023\n2024\n2025\n2026\n2027\n2028\n2029\n"
"2030\n2031\n2032\n2033\n2034\n2035\n2036\n2037\n2038\n2039\n"
"2040\n2041\n2042\n2043\n2044\n2045\n2046\n2047\n2048\n2049\n"
"2050\n2051\n2052\n2053\n2054\n2055\n2056\n2057\n2058\n2059\n"
"2060\n2061\n2062\n2063\n2064\n2065\n2066\n2067\n2068\n2069\n"
"2070\n2071\n2072\n2073\n2074\n2075\n2076\n2077\n2078\n2079\n"
"2080\n2081\n2082\n2083\n2084\n2085\n2086\n2087\n2088\n2089\n"
"2090\n2091\n2092\n2093\n2094\n2095\n2096\n2097\n2098\n2099"
};
lv_obj_t* lv_calendar_header_dropdown_create(lv_obj_t* parent)
{
lv_obj_t* obj = lv_obj_class_create_obj(&lv_calendar_header_dropdown_class, parent);
lv_obj_class_init_obj(obj);
return obj;
}
static void my_constructor(const lv_obj_class_t* class_p, lv_obj_t* obj)
{
LV_TRACE_OBJ_CREATE("begin");
LV_UNUSED(class_p);
lv_obj_t* calendar = lv_obj_get_parent(obj);
lv_obj_move_to_index(obj, 0);
lv_obj_set_flex_flow(obj, LV_FLEX_FLOW_ROW);
lv_obj_t* year_dd = lv_dropdown_create(obj);
lv_dropdown_set_options(year_dd, year_list);
lv_obj_add_event_cb(year_dd, year_event_cb, LV_EVENT_VALUE_CHANGED, calendar);
lv_obj_set_flex_grow(year_dd, 1);
lv_obj_t* month_dd = lv_dropdown_create(obj);
lv_dropdown_set_options(month_dd, month_list);
lv_obj_add_event_cb(month_dd, month_event_cb, LV_EVENT_VALUE_CHANGED, calendar);
lv_obj_set_flex_grow(month_dd, 1);
lv_obj_add_event_cb(obj, value_changed_event_cb, LV_EVENT_VALUE_CHANGED, NULL);
lv_event_send(obj, LV_EVENT_VALUE_CHANGED, NULL);
}
static void month_event_cb(lv_event_t* e)
{
lv_obj_t* dropdown = lv_event_get_target(e);
lv_obj_t* calendar = lv_event_get_user_data(e);
uint16_t sel = lv_dropdown_get_selected(dropdown);
const lv_calendar_date_t* d;
d = lv_calendar_get_showed_date(calendar);
lv_calendar_date_t newd = *d;
newd.month = sel + 1;
newd.day = 1;
lv_calendar_set_showed_date(calendar, newd.year, newd.month);
lv_calendar_set_today_date(calendar, newd.year, newd.month, newd.day);
lv_event_send(calendar, LV_EVENT_VALUE_CHANGED, NULL);
}
static void year_event_cb(lv_event_t* e)
{
lv_obj_t* dropdown = lv_event_get_target(e);
lv_obj_t* calendar = lv_event_get_user_data(e);
uint16_t sel = lv_dropdown_get_selected(dropdown);
const lv_calendar_date_t* d;
d = lv_calendar_get_showed_date(calendar);
lv_calendar_date_t newd = *d;
newd.year = 2023 + sel;
newd.day = 1;
lv_calendar_set_showed_date(calendar, newd.year, newd.month);
lv_calendar_set_today_date(calendar, newd.year, newd.month, newd.day);
lv_event_send(calendar, LV_EVENT_VALUE_CHANGED, NULL);
}
static void value_changed_event_cb(lv_event_t* e)
{
lv_obj_t* header = lv_event_get_target(e);
lv_obj_t* calendar = lv_obj_get_parent(header);
const lv_calendar_date_t* cur_date = lv_calendar_get_showed_date(calendar);
lv_obj_t* year_dd = lv_obj_get_child(header, 0);
lv_dropdown_set_selected(year_dd, cur_date->year - 2023);
lv_obj_t* month_dd = lv_obj_get_child(header, 1);
lv_dropdown_set_selected(month_dd, cur_date->month - 1);
}
#endif
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151