这几天由于开发任务的需要,需要自定义实现一个radio button的功能,所以通过研究button添加LV_OBJ_FLAG_CHECKABLE属性实现了一个简单的radio button,下面直接上代码

#include <string.h>
#include <stdlib.h>
#include "lvgl/lvgl.h"


#define THIS_FILE  "inpatient_cost.c"


static void radio_btn_event_callback(lv_event_t* event)
{
    if (event == NULL)
    {
        printf("[%s:%d] event is NULL\n", __FUNCTION__, __LINE__);
        return ;
    }

    uint32_t* active_id = (uint32_t*)lv_event_get_user_data(event); //获取用户数据
    lv_obj_t* obj_cont = lv_event_get_current_target(event); // 获取当前目标事件的布局对象
    lv_obj_t* obj_active = lv_event_get_target(event);//获取产生事件的对象
    lv_obj_t* obj_old = lv_obj_get_child(obj_cont, *active_id); //根据id序号获取子对象

    if (obj_active == obj_cont)
    {
        return; //不处理layout布局对象产生的事件
    }

    lv_obj_clear_state(obj_old, LV_STATE_CHECKED); //清除先前的选择状态
    lv_obj_add_state(obj_active, LV_STATE_CHECKED); // 设置点击的对象为选中状态

    // 选中和非选中字体颜色要和背景颜色互换
    lv_obj_t * obj_old_child = lv_obj_get_child(obj_old, 0);
    if (obj_old_child != NULL)
    {
        lv_obj_set_style_text_color(obj_old_child, lv_color_hex(0x1985CE), 0);
    }

    lv_obj_t * obj_active_child = lv_obj_get_child(obj_active, 0);
    if (obj_active_child != NULL)
    {
        lv_obj_set_style_text_color(obj_active_child, lv_color_hex(0xFFFFFF), 0);
    }

    *active_id = lv_obj_get_index(obj_active); //获取选中对象的id序号并保存在active_id所指向的内存中
    printf("[%s::%s:%d] active_id:%u\n", THIS_FILE, __FUNCTION__, __LINE__, (uint32_t)*active_id);
}


static void lv_gui_radio_button_test()
{
    static uint32_t active_index = 0;

    lv_obj_t *parent = lv_scr_act();

    // 整个页面的白色背景
    lv_obj_t * obj_page_bg = lv_obj_create(parent);
    if (obj_page_bg == NULL)
    {
        printf("[%s::%s:%d] obj_page_bg create failed\n", THIS_FILE, __FUNCTION__, __LINE__);
        return ;
    }

    lv_obj_set_style_radius(obj_page_bg, 10, 0); //圆角弧度
    lv_obj_set_style_pad_all(obj_page_bg, 0, 0); //内部padding填充
    lv_obj_set_style_border_width(obj_page_bg, 0, 0); //边框宽度
    lv_obj_set_style_bg_color(obj_page_bg, lv_color_hex(0xFFFFFF), 0); // 背景颜色设置
    lv_obj_set_size(obj_page_bg, 800, 480);
    lv_obj_align(obj_page_bg, LV_ALIGN_TOP_LEFT, 0, 0);

    // 表头按键容器
    lv_obj_t* obj_btn_cont = lv_obj_create(obj_page_bg);
    if (obj_btn_cont == NULL)
    {
        printf("[%s::%s:%d] obj_btn_cont create failed\n", THIS_FILE, __FUNCTION__, __LINE__);
        return ;
    }

    lv_obj_set_style_radius(obj_btn_cont, 0, 0); //圆角弧度
    lv_obj_set_style_pad_all(obj_btn_cont, 0, 0); //内部padding填充
    //lv_obj_set_flex_flow(obj_btn_cont, LV_FLEX_FLOW_ROW);//设置布局为
    lv_obj_set_size(obj_btn_cont, 288, 36); //设置大小
    lv_obj_align(obj_btn_cont, LV_ALIGN_TOP_LEFT, 150, 10);
    lv_obj_add_event_cb(obj_btn_cont, radio_btn_event_callback, LV_EVENT_CLICKED, &active_index);

    // 每日费用明细按键
    lv_obj_t* btn_daily_cost = lv_btn_create(obj_btn_cont);  //在parent对象上创建checkbox对象
    if (btn_daily_cost == NULL)
    {
        printf("[%s::%s:%d] btn_daily_cost create failed\n", THIS_FILE, __FUNCTION__, __LINE__);
        return ;
    }

    lv_obj_add_flag(btn_daily_cost, LV_OBJ_FLAG_CHECKABLE); // 添加可选中属性
    lv_obj_add_flag(btn_daily_cost, LV_OBJ_FLAG_EVENT_BUBBLE); // 添加事件传导
    lv_obj_set_style_pad_all(btn_daily_cost, 0, 0); //内部padding填充
    lv_obj_set_style_radius(btn_daily_cost, 0, 0); //圆角弧度
    lv_obj_set_style_bg_opa(btn_daily_cost, LV_OPA_COVER, 0);
    lv_obj_set_style_bg_color(btn_daily_cost, lv_color_hex(0xFFFFFF), 0);
    lv_obj_set_style_bg_color(btn_daily_cost, lv_color_hex(0x0D5EBF), LV_STATE_CHECKED); // 选中时的背景颜色
    lv_obj_set_size(btn_daily_cost, 140, 32); //设置大小
    lv_obj_align(btn_daily_cost, LV_ALIGN_TOP_LEFT, 0, 0);

    lv_obj_t * btn_daily_cost_label = lv_label_create(btn_daily_cost);
    if (btn_daily_cost_label == NULL)
    {
        printf("[%s::%s:%d] btn_daily_cost_label create failed\n", THIS_FILE, __FUNCTION__, __LINE__);
        return -1;
    }

    lv_font_t* font_18 = &lv_font_montserrat_18;
    lv_obj_set_style_text_font(btn_daily_cost_label, (const lv_font_t *)font_18, 0);
    lv_obj_set_style_text_color(btn_daily_cost_label, lv_color_hex(0xFFFFFF), 0); // 设置文本颜色
    //lv_obj_set_style_text_color(btn_daily_cost_label, lv_color_hex(0x1985CE), 0);
    lv_label_set_text(btn_daily_cost_label, "Daily Cost"); // 设置文本内容 返回
    lv_obj_align(btn_daily_cost_label, LV_ALIGN_CENTER, 0, 0); // 设置对齐

    // 药品清单按键
    lv_obj_t* btn_medcine_list = lv_btn_create(obj_btn_cont);//lv_checkbox_create(obj_btn_cont);  //在parent对象上创建checkbox对象
    if (btn_medcine_list == NULL)
    {
        printf("[%s::%s:%d] btn_medcine_list create failed\n", THIS_FILE, __FUNCTION__, __LINE__);
        return ;
    }

    lv_obj_add_flag(btn_medcine_list, LV_OBJ_FLAG_CHECKABLE); // 添加可选中属性
    lv_obj_add_flag(btn_medcine_list, LV_OBJ_FLAG_EVENT_BUBBLE); // 添加事件传导
    lv_obj_set_style_pad_all(btn_medcine_list, 0, 0); //内部padding填充
    lv_obj_set_style_radius(btn_medcine_list, 0, 0); //圆角弧度
    lv_obj_set_style_bg_opa(btn_medcine_list, LV_OPA_COVER, 0);
    lv_obj_set_style_bg_color(btn_medcine_list, lv_color_hex(0xFFFFFF), 0);
    lv_obj_set_style_bg_color(btn_medcine_list, lv_color_hex(0x0D5EBF), LV_STATE_CHECKED); // 选中时的背景颜色
    lv_obj_set_size(btn_medcine_list, 142, 32); //设置大小
    lv_obj_align(btn_medcine_list, LV_ALIGN_TOP_LEFT, 142, 0);

    lv_obj_t * btn_medcine_list_label = lv_label_create(btn_medcine_list);
    if (btn_medcine_list_label == NULL)
    {
        printf("[%s::%s:%d] btn_medcine_list_label create failed\n", THIS_FILE, __FUNCTION__, __LINE__);
        return -1;
    }

    lv_obj_set_style_text_font(btn_medcine_list_label, (const lv_font_t *)font_18, 0);
    //lv_obj_set_style_text_color(btn_medcine_list_label, lv_color_hex(0xFFFFFF), LV_STATE_CHECKED); // 设置文本颜色
    lv_obj_set_style_text_color(btn_medcine_list_label, lv_color_hex(0x1985CE), 0);
    lv_label_set_text(btn_medcine_list_label, "Medcine List"); // 设置文本内容 返回
    lv_obj_align(btn_medcine_list_label, LV_ALIGN_CENTER, 0, 0); // 设置对齐

    lv_obj_t *cur_obj = lv_obj_get_child(obj_btn_cont, active_index);
    if (cur_obj != NULL)
    {
        lv_obj_add_state(lv_obj_get_child(obj_btn_cont, active_index), LV_STATE_CHECKED);
    }
}

下面是运行效果:

Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐