Hello Dear friends,
I was trying to implement a radio button by the means of the checkbox widget. I know that this can be done with the btnmatrix widget but the checkbox is something else.
My first method is to use the evil Global variable. The code is as follows.
struct Object
{
lv_obj_t * Selection;
unsigned char num;
};
struct Object ViewContent[4];
static void VIEW_event_handler(lv_event_t * e)
{
lv_event_code_t code = lv_event_get_code(e);
struct Object *ViewTemp = lv_event_get_user_data(e);
unsigned char userdata = ViewTemp->num;
if(code == LV_EVENT_RELEASED)
{
lv_obj_add_state (ViewTemp.Selection, LV_STATE_CHECKED);
for (unsigned char i=0;i<4;i++)
if (i!=userdata)
lv_obj_clear_state(ViewContent[i].Selection, LV_STATE_CHECKED);
}
}
lv_obj_t* CreateScreenMenuView(void)
{
ScreenMenu = lv_obj_create(NULL);
lv_scr_load_anim(ScreenMenu,LV_SCR_LOAD_ANIM_FADE_ON, 200, 0, true);
for (unsigned char i = 0;i<4;i++)
{
ViewContent[i].Selection = lv_checkbox_create(ScreenMenu);
ViewContent[i].num = i+1;
lv_obj_add_event_cb(ViewContent[i].Selection,VIEW_event_handler,LV_EVENT_RELEASED,&ViewContent[i]);
}
lv_checkbox_set_text(ViewContent[0].Selection,"E1");
lv_checkbox_set_text(ViewContent[1].Selection,"E2");
lv_checkbox_set_text(ViewContent[2].Selection,"E3");
lv_checkbox_set_text(ViewContent[3].Selection,"E4");
lv_obj_add_state(ViewContent[0].Selection,LV_STATE_CHECKED);
lv_obj_set_pos(ViewContent[0].Selection,100,100);
lv_obj_align_to(ViewContent[1].Selection,ViewContent[0].Selection,LV_ALIGN_OUT_BOTTOM_LEFT,0,30);
lv_obj_align_to(ViewContent[2].Selection,ViewContent[1].Selection,LV_ALIGN_OUT_BOTTOM_LEFT,0,30);
lv_obj_align_to(ViewContent[3].Selection,ViewContent[2].Selection,LV_ALIGN_OUT_BOTTOM_LEFT,0,30);
CreateReturnButton(ScreenMenu);
return ScreenMenu;
}
The code above works, but there are some problems. First and foremost, the problem is with static and non-dynamic definitions. By which I mean via the above method, the number of buttons should be determined in advance. Second, the variable containing is global.
Then, my next try was as follows
typedef struct
{
unsigned char index;
lv_obj_t * radiobtnVIEW[4];
}AssetVIEW
static void VIEW_event_handler(lv_event_t * e)
{
lv_event_code_t code = lv_event_get_code(e);
lv_obj_t * target = lv_event_get_current_target(e);
AssetVIEW * ViewTemp = lv_event_get_user_data(e);
if(code == LV_EVENT_RELEASED)
{
for (unsigned char i=0;i<4;i++)
lv_obj_clear_state(ViewTemp->radiobtnVIEW[i], LV_STATE_CHECKED);
lv_obj_add_state (target, LV_STATE_CHECKED);
for (unsigned char i=0;i<4;i++)
if(lv_obj_has_state (ViewTemp->radiobtnVIEW[i], LV_STATE_CHECKED))
{
ViewTemp->index = i;
break;
}
}
ViewTemp->index;
}
lv_obj_t* CreateScreenMenuView(void)
{
ScreenMenu = lv_obj_create(NULL);
lv_scr_load_anim(ScreenMenu,LV_SCR_LOAD_ANIM_FADE_ON, 200, 0, true);
static lv_style_t style;
lv_style_init(&style);
lv_style_set_radius(&style,10);
static AssetVIEW ViewContent;
for (unsigned char i = 0;i<4;i++)
{
ViewContent.radiobtnVIEW[i] = lv_checkbox_create(ScreenMenu);
lv_obj_add_style(ViewContent.radiobtnVIEW[i],&style,LV_PART_INDICATOR);
lv_obj_add_event_cb(ViewContent.radiobtnVIEW[i],VIEW_event_handler,LV_EVENT_RELEASED,&ViewContent);
}
lv_checkbox_set_text(ViewContent.radiobtnVIEW[0],"E1");
lv_checkbox_set_text(ViewContent.radiobtnVIEW[1],"E2");
lv_checkbox_set_text(ViewContent.radiobtnVIEW[2],"E3");
lv_checkbox_set_text(ViewContent.radiobtnVIEW[3],"E4");
lv_obj_add_state(ViewContent.radiobtnVIEW[0],LV_STATE_CHECKED);
lv_obj_set_pos(ViewContent.radiobtnVIEW[0],100,100);
lv_obj_align_to(ViewContent.radiobtnVIEW[1],ViewContent.radiobtnVIEW[0],LV_ALIGN_OUT_BOTTOM_LEFT,0,30);
lv_obj_align_to(ViewContent.radiobtnVIEW[2],ViewContent.radiobtnVIEW[1],LV_ALIGN_OUT_BOTTOM_LEFT,0,30);
lv_obj_align_to(ViewContent.radiobtnVIEW[3],ViewContent.radiobtnVIEW[2],LV_ALIGN_OUT_BOTTOM_LEFT,0,30);
CreateReturnButton(ScreenMenu);
return ScreenMenu;
}
The last code problem is non-dynamic initialization. By which I mean the number of the radio buttons should be determined in advance.
Then I looked for the lv_group functionality. But I couldn’t get deep into it. Your help is really appreciated.