LVGL8.x 自带的label 是没有点击响应的功能,即使加了lv_obj_add_event_cb 也不起作用,为了解决这个问题,我们使用了按钮控件去模拟纯label的效果;有了这个demo用户就可以实现类似超链接 点击一个文字就跳转到某个页面的功能。
static void tp_event_handler(lv_event_t * e)
{
lv_event_code_t code = lv_event_get_code(e);
uint8_t* btn_id = e->user_data;
if(code == LV_EVENT_CLICKED) {
printf("[Click] btn_id = %d \n",*btn_id);
}
else if(code == LV_EVENT_VALUE_CHANGED) {
printf("[Toggle] btn_id = %d \n",*btn_id);
}
}
typedef struct
{
uint8_t txt_id;
int x;
int y;
char* txt_str;
void(*event_handler)(lv_event_t* e);
lv_obj_t* self_obj;
} QUI_TXT;
#define NUM_OBJ (5)
static QUI_TXT lqui_txt[NUM_OBJ]=
{
{0,50,30,"China",tp_event_handler,NULL},
{1,50,60,"England",tp_event_handler,NULL},
{2,50,90,"Brazil",tp_event_handler,NULL},
{3,50,120,"Mexico",tp_event_handler,NULL},
{4,50,150,"Egypt",tp_event_handler,NULL},
};
static int create_tp_txt(lv_obj_t* parent_obj,QUI_TXT* qTxt)
{
lv_obj_t* btn = lv_btn_create(parent_obj);
if (btn==NULL)
{
return 0;
}
lv_obj_set_style_bg_opa(btn, 0, 0);
lv_obj_set_style_shadow_opa(btn, 0, 0);
lv_obj_set_style_text_color(btn, lv_color_hex(0x000000), 0);
lv_obj_set_pos(btn, qTxt->x,qTxt->y);
lv_obj_t* label = lv_label_create(btn);
if (qTxt->txt_str)
{
lv_label_set_text(label,qTxt->txt_str);
}
if (qTxt->event_handler != NULL)
{
lv_obj_add_event_cb(btn, qTxt->event_handler, LV_EVENT_ALL, (void*)&qTxt->txt_id);
}
qTxt->self_obj = btn;
return 1;
}
void lv_example_tp_text(void)
{
lv_obj_t* parent_obj = lv_scr_act();
for(int i=0;i< NUM_OBJ;i++)
create_tp_txt(parent_obj, &lqui_txt[i]);
lv_obj_add_flag(lqui_txt[0].self_obj,LV_OBJ_FLAG_CHECKABLE);//按钮1做特殊效果
lv_timer_handler();
return;
}
demo里面 点击不同的文字,日志会输出不同的txt_id 。