zl程序教程

您现在的位置是:首页 >  其它

当前栏目

LVGL 8.2.0之Calendar

LVGL calendar 8.2
2023-09-14 09:06:41 时间

事件处理函数

static void event_handler(lv_event_t* e)
{
    lv_event_code_t code = lv_event_get_code(e); //获取事件code
    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); //打印日历信息
        }
    }
}
  • 创建Calendar控件
static void lv_example_calendar_1(void)
{
    lv_obj_t* calendar = lv_calendar_create(lv_scr_act()); //创建Calendar对象
    lv_obj_set_size(calendar, 185, 185); //设置大小
    lv_obj_align(calendar, LV_ALIGN_CENTER, 0, 27); //居中y向下27PX显示
    lv_obj_add_event_cb(calendar, event_handler, LV_EVENT_ALL, NULL); //注册所有事件
    lv_calendar_set_today_date(calendar, 2021, 02, 23); //设置今天显示日期为2021-02-23
    lv_calendar_set_showed_date(calendar, 2021, 02);//设置显示日期为2021年2月

    /*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); //设置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
    lv_calendar_set_showed_date(calendar, 2021, 10); //重新设置显示日期为2021年10月
}

  • 调用函数lv_example_calendar_1,运行效果图
    在这里插入图片描述