当前位置:网站首页>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);
}
运行效果

边栏推荐
- 苹果5G芯片被曝研发失败,QQ密码bug引热议,蔚来回应做空传闻,今日更多大新闻在此...
- Use keil5 software to simulate and debug gd32f305 from 0
- 05_ Node JS file management module FS
- CSDN博客运营团队2022年H1总结
- Didi open source agile test case management platform!
- 历史上的今天:微软收购 PowerPoint 开发商;SGI 和 MIPS 合并
- 滴滴开源敏捷测试用例管理平台!
- Go -- maximum heap and minimum heap
- Ant s19xp appeared in 140t, why is it called the computing power ceiling by the world
- GD32 RT-Thread RTC驱动函数
猜你喜欢

19:00 p.m. tonight, knowledge empowerment phase 2 live broadcast - control panel interface design of openharmony smart home project

I found a wave of "alchemy artifact" in the goose factory. The developer should pack it quickly

CVPR 2022 | Tsinghua & bytek & JD put forward BRT: Bridging Transformer for vision and point cloud 3D target detection

Test memory read rate

pytorch 笔记 torch.nn.BatchNorm1d
[email protected]+ Alibaba cloud +nbiot+dht11+bh1750+ soil moisture sensor +oled"/>Skill sorting [email protected]+ Alibaba cloud +nbiot+dht11+bh1750+ soil moisture sensor +oled

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

苹果5G芯片被曝研发失败,QQ密码bug引热议,蔚来回应做空传闻,今日更多大新闻在此...
[email protected] control a dog's running on OLED"/>Skill combing [email protected] control a dog's running on OLED

记一次实习的经历,趟坑必备(一)
随机推荐
Smith chart view of semi steel coaxial RF line and RF line matching calibration of network analyzer e5071c
断路器HystrixCircuitBreaker
WGet -- 404 not found due to spaces in URL
"Kunming City coffee map" was opened again, and coffee brought the city closer
TypeScript–es5中的类,继承,静态方法
GD32 RT-Thread DAC驱动函数
mysql数据库基础:TCL事务控制语言
Circuit breaker hystrixcircuitbreaker
【Rust日报】2021-01-22 首份Rust月刊杂志邀请大家一起参与
7 大轻量易用的工具,给开发者减压提效,助力企业敏捷上云 | Techo Day 精彩回顾...
GD32 RT-Thread RTC驱动函数
数学知识复习:第二型曲线积分
Collectors.toMap应用
智能DNA分子纳米机器人模型来了
前嗅ForeSpider教程:抽取数据
技能梳理[email protected]+阿里云+nbiot+dht11+bh1750+土壤湿度传感器+oled
ArcGIS Pro scripting tool (5) - delete duplicates after sorting
我在鹅厂淘到了一波“炼丹神器”,开发者快打包
潘多拉 IOT 开发板学习(HAL 库)—— 实验1 跑马灯(RGB)实验(学习笔记)
Yixian e-commerce released its first quarterly report: adhere to R & D and brand investment to achieve sustainable and high-quality development