当前位置:网站首页>LVGL 8.2 Checkboxes as radio buttons
LVGL 8.2 Checkboxes as radio buttons
2022-06-30 10:08:00 【仙剑情缘】
变量定义
static lv_style_t style_radio;
static lv_style_t style_radio_chk;
static uint32_t active_index_1 = 0;
static uint32_t active_index_2 = 0;
事件处理回调函数
static void radio_event_handler(lv_event_t* e)
{
uint32_t* active_id = lv_event_get_user_data(e); //获取用户数据
lv_obj_t* cont = lv_event_get_current_target(e); // 获取当前目标事件的布局对象
lv_obj_t* act_cb = lv_event_get_target(e);//获取产生事件的对象
lv_obj_t* old_cb = lv_obj_get_child(cont, *active_id); //根据id序号获取子对象
/*Do nothing if the container was clicked*/
if (act_cb == cont) return; //不处理layout布局对象产生的事件
lv_obj_clear_state(old_cb, LV_STATE_CHECKED); //清除先前的选择状态
lv_obj_add_state(act_cb, LV_STATE_CHECKED); // 设置点击的对象为选中状态
*active_id = lv_obj_get_index(act_cb); //获取选中对象的id序号并保存在active_id所指向的内存中
LV_LOG_USER("Selected radio buttons: %d, %d", (int)active_index_1, (int)active_index_2); //打印active_index_1,active_index_2
}
radio创建
static void radiobutton_create(lv_obj_t* parent, const char* txt)
{
lv_obj_t* obj = lv_checkbox_create(parent); //在parent对象上创建checkbox对象
lv_checkbox_set_text(obj, txt); // 设置文本为txt指针中的内容
lv_obj_add_flag(obj, LV_OBJ_FLAG_EVENT_BUBBLE); // 添加LV_OBJ_FLAG_EVENT_BUBBLE标志
lv_obj_add_style(obj, &style_radio, LV_PART_INDICATOR); //obj对象的LV_PART_INDICATO属性部分使用style_radio
lv_obj_add_style(obj, &style_radio_chk, LV_PART_INDICATOR | LV_STATE_CHECKED); // obj对象的LV_PART_INDICATO和LV_STATE_CHECKED属性部分使用style_radio_chk
}
Checkboxes as radio buttons
/** * Checkboxes as radio buttons */
static void lv_example_checkbox_2(void)
{
/* The idea is to enable `LV_OBJ_FLAG_EVENT_BUBBLE` on checkboxes and process the * `LV_EVENT_CLICKED` on the container. * A variable is passed as event user data where the index of the active * radiobutton is saved */
lv_style_init(&style_radio); //初时化style_radio
lv_style_set_radius(&style_radio, LV_RADIUS_CIRCLE); //设置倒角为圆形
lv_style_init(&style_radio_chk); //初时化style_radio_chk
lv_style_set_bg_img_src(&style_radio_chk, NULL); //设置背景图标资源为NULL
uint32_t i;
char buf[32];
lv_obj_t* cont1 = lv_obj_create(lv_scr_act()); //创建对象用于layout
lv_obj_set_flex_flow(cont1, LV_FLEX_FLOW_COLUMN);//设置布局为LV_FLEX_FLOW_COLUMN方式
lv_obj_set_size(cont1, lv_pct(40), lv_pct(80)); //设置大小
lv_obj_add_event_cb(cont1, radio_event_handler, LV_EVENT_CLICKED, &active_index_1); // 添加LV_EVENT_CLICKED事件,用户自定义数据为active_index_1
for (i = 0; i < 5; i++) {
lv_snprintf(buf, sizeof(buf), "A %d", (int)i + 1);
radiobutton_create(cont1, buf); //共创建5个radio按键
}
/*Make the first checkbox checked*/
lv_obj_add_state(lv_obj_get_child(cont1, 0), LV_STATE_CHECKED); //选中第1个radio
lv_obj_t* cont2 = lv_obj_create(lv_scr_act()); //创建对象用于layout
lv_obj_set_flex_flow(cont2, LV_FLEX_FLOW_COLUMN);//设置布局为LV_FLEX_FLOW_COLUMN方式
lv_obj_set_size(cont2, lv_pct(40), lv_pct(80)); //设置大小
lv_obj_set_x(cont2, lv_pct(50)); //设置cont2布局x位置为屏慕的50%处
lv_obj_add_event_cb(cont2, radio_event_handler, LV_EVENT_CLICKED, &active_index_2);// 添加LV_EVENT_CLICKED事件,用户自定义数据为active_index_2
for (i = 0; i < 3; i++) {
lv_snprintf(buf, sizeof(buf), "B %d", (int)i + 1);
radiobutton_create(cont2, buf); //共创建3个radio按键
}
/*Make the first checkbox checked*/
lv_obj_add_state(lv_obj_get_child(cont2, 0), LV_STATE_CHECKED);//选中第1个radio
}
运行效果

将radiobutton_create函数中的lv_obj_add_flag注释掉
static void radiobutton_create(lv_obj_t* parent, const char* txt)
{
lv_obj_t* obj = lv_checkbox_create(parent);
lv_checkbox_set_text(obj, txt);
// lv_obj_add_flag(obj, LV_OBJ_FLAG_EVENT_BUBBLE);
lv_obj_add_style(obj, &style_radio, LV_PART_INDICATOR);
lv_obj_add_style(obj, &style_radio_chk, LV_PART_INDICATOR | LV_STATE_CHECKED);
}
运行效果

边栏推荐
- Apple's 5g chip was revealed to have failed in research and development, and the QQ password bug caused heated discussion. Wei Lai responded to the short selling rumors. Today, more big news is here
- Foster design method
- 【Rust每周一库】num-bigint - 大整数
- MySQL从入门到精通50讲(三十二)-ScyllaDB生产环境集群搭建
- 技能梳理[email protected]在oled上控制一条狗的奔跑
- WGet -- 404 not found due to spaces in URL
- CSDN blog operation team 2022 H1 summary
- IPhone address book import into Excel
- sCrypt 中的 ECDSA 签名验证
- 【Rust日报】2021-01-23 几个新库发布
猜你喜欢

今晚19:00知识赋能第2期直播丨OpenHarmony智能家居项目之控制面板界面设计

再测云原生数据库性能:PolarDB依旧最强,TDSQL-C、GaussDB变化不大

Viewing technological changes through Huawei Corps (V): smart Park

matplotlib 笔记: contourf & contour

Implementation of monitor program with assembly language

Robotframework learning notes: environment installation and robotframework browser plug-in installation

Pytorch notes torch nn. BatchNorm1d

ArcGIS Pro scripting tool (6) -- repairing CAD layer data sources

从0使用keil5软件仿真调试GD32F305
[email protected]+adxl345+ Motor vibration + serial port output"/>Skill sorting [email protected]+adxl345+ Motor vibration + serial port output
随机推荐
半钢同轴射频线的史密斯圆图查看和网络分析仪E5071C的射频线匹配校准
【Rust日报】2021-01-23 几个新库发布
My in-depth remote office experience | community essay solicitation
The latest SCI impact factor release: the highest score of domestic journals is 46! Netizen: I understand if
文件共享服务器
pytorch 筆記 torch.nn.BatchNorm1d
Getting started with X86 - take over bare metal control
Leetcode question brushing (III) -- binary search (go Implementation)
R语言aov函数进行重复测量方差分析(Repeated measures ANOVA、其中一个组内因素和一个组间因素)、分别使用interaction.plot函数和boxplot对交互作用进行可视化
The programmer was beaten.
ArcGIS Pro + PS 矢量化用地规划图
59 websites programmers need to know
数学知识复习:第二型曲线积分
无心剑中译狄金森《灵魂择其伴侣》
机器学习面试准备(一)KNN
MySQL从入门到精通50讲(三十二)-ScyllaDB生产环境集群搭建
Overview of currency
如何解决跨域
ionic4 ion-reorder-group组件拖拽改变item顺序
nvm、nrm、npx使用(安装、基本命令、参数、curl、wget)