LVGL_基础控件checkbox
1、创建checkbox
lv_obj_t * cb = lv_checkbox_create(lv_scr_act());
lv_checkbox_set_text(cb, "100ASK LVGL Tutorial" LV_SYMBOL_PLAY);
lv_obj_center(cb);
2、修改勾选框和文字之间的距离
lv_obj_set_style_pad_column(cb, 100, 0);
3、修改勾选框和文字的大小
lv_obj_set_style_text_font(cb, &lv_font_montserrat_32, 0);
lv_obj_set_style_pad_all(cb, 20, LV_PART_INDICATOR);
4、修改复选框的背景颜色
lv_obj_set_style_bg_opa(cb, 100, LV_PART_MAIN);
lv_obj_set_style_bg_color(cb, lv_color_hex(0xc43e1c), LV_PART_MAIN);
5、修改复选框选中和未选中时候的颜色
lv_obj_set_style_bg_color(cb, lv_color_hex(0xc43e1c), LV_PART_INDICATOR);
lv_obj_set_style_bg_color(cb, lv_color_hex(0x7719aa), LV_PART_INDICATOR | LV_STATE_CHECKED);
6、设置默认选中切不能更改
lv_obj_add_state(cb, LV_STATE_CHECKED);
lv_obj_add_state(cb, LV_STATE_CHECKED | LV_STATE_DISABLED);
7、设置默认不选中且不能更改
lv_obj_clear_state(cb, LV_STATE_CHECKED);
lv_obj_add_state(cb, LV_STATE_DISABLED);
8、清除禁用状态
lv_obj_clear_state(cb, LV_STATE_DISABLED);
9、添加事件
lv_obj_add_event_cb(cb, cb_event_handler, LV_EVENT_VALUE_CHANGED, NULL);
static void cb_event_handler(lv_event_t * e)
{
lv_event_code_t code = lv_event_get_code(e);
lv_obj_t *cb = lv_event_get_target(e);
if (code == LV_EVENT_VALUE_CHANGED)
{
const char * txt = lv_checkbox_get_text(cb);
LV_LOG_USER("%s: %s", txt, lv_obj_has_state(cb, LV_STATE_CHECKED) ? "CHECKED" : "UNCHECKED");
}
}