mirror of
https://github.com/lvgl/lvgl.git
synced 2026-05-25 18:29:26 +08:00
feat(obj): add LV_OBJ_FLAG_RADIO_BUTTON to easily create radio buttons (#9328)
This commit is contained in:
committed by
GitHub
parent
d2bbbcc854
commit
721029ac22
@@ -3,34 +3,16 @@
|
||||
|
||||
static lv_style_t style_radio;
|
||||
static lv_style_t style_radio_chk;
|
||||
static int32_t active_index_1 = 0;
|
||||
static int32_t active_index_2 = 0;
|
||||
|
||||
static void radio_event_handler(lv_event_t * e)
|
||||
static void event_cb(lv_event_t * e)
|
||||
{
|
||||
int32_t * active_id = (int32_t *)lv_event_get_user_data(e);
|
||||
lv_obj_t * cont = (lv_obj_t *)lv_event_get_current_target(e);
|
||||
lv_obj_t * act_cb = lv_event_get_target_obj(e);
|
||||
lv_obj_t * old_cb = lv_obj_get_child(cont, *active_id);
|
||||
|
||||
/*Do nothing if the container was clicked*/
|
||||
if(act_cb == cont) return;
|
||||
|
||||
lv_obj_remove_state(old_cb, LV_STATE_CHECKED); /*Uncheck the previous radio button*/
|
||||
lv_obj_add_state(act_cb, LV_STATE_CHECKED); /*Check the current radio button*/
|
||||
|
||||
*active_id = lv_obj_get_index(act_cb);
|
||||
|
||||
LV_LOG_USER("Selected radio buttons: %d, %d", (int)active_index_1, (int)active_index_2);
|
||||
}
|
||||
|
||||
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);
|
||||
lv_obj_t * obj = lv_event_get_target_obj(e);
|
||||
if(lv_obj_has_state(obj, LV_STATE_CHECKED)) {
|
||||
LV_LOG_USER("%s is selected.", lv_checkbox_get_text(obj));
|
||||
}
|
||||
else {
|
||||
LV_LOG_USER("%s is de-selected.", lv_checkbox_get_text(obj));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -38,46 +20,37 @@ static void radiobutton_create(lv_obj_t * parent, const char * txt)
|
||||
*/
|
||||
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);
|
||||
lv_style_set_radius(&style_radio, LV_RADIUS_CIRCLE);
|
||||
|
||||
lv_style_init(&style_radio_chk);
|
||||
lv_style_set_bg_image_src(&style_radio_chk, NULL);
|
||||
|
||||
|
||||
lv_obj_t * cont = lv_obj_create(lv_screen_active());
|
||||
lv_obj_set_flex_flow(cont, LV_FLEX_FLOW_COLUMN);
|
||||
lv_obj_set_size(cont, LV_SIZE_CONTENT, LV_SIZE_CONTENT);
|
||||
lv_obj_center(cont);
|
||||
|
||||
uint32_t i;
|
||||
char buf[32];
|
||||
|
||||
lv_obj_t * cont1 = lv_obj_create(lv_screen_active());
|
||||
lv_obj_set_flex_flow(cont1, 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);
|
||||
|
||||
for(i = 0; i < 5; i++) {
|
||||
lv_snprintf(buf, sizeof(buf), "A %d", (int)i + 1);
|
||||
radiobutton_create(cont1, buf);
|
||||
lv_snprintf(buf, sizeof(buf), "Radio button %d", (int)i + 1);
|
||||
|
||||
}
|
||||
/*Make the first checkbox checked*/
|
||||
lv_obj_add_state(lv_obj_get_child(cont1, 0), LV_STATE_CHECKED);
|
||||
lv_obj_t * obj = lv_checkbox_create(cont);
|
||||
lv_checkbox_set_text(obj, buf);
|
||||
|
||||
lv_obj_t * cont2 = lv_obj_create(lv_screen_active());
|
||||
lv_obj_set_flex_flow(cont2, LV_FLEX_FLOW_COLUMN);
|
||||
lv_obj_set_size(cont2, lv_pct(40), lv_pct(80));
|
||||
lv_obj_set_x(cont2, lv_pct(50));
|
||||
lv_obj_add_event_cb(cont2, radio_event_handler, LV_EVENT_CLICKED, &active_index_2);
|
||||
lv_obj_add_event_cb(obj, event_cb, LV_EVENT_VALUE_CHANGED, NULL);
|
||||
|
||||
for(i = 0; i < 3; i++) {
|
||||
lv_snprintf(buf, sizeof(buf), "B %d", (int)i + 1);
|
||||
radiobutton_create(cont2, buf);
|
||||
/*This makes the checkboxes act as radio buttons*/
|
||||
lv_obj_add_flag(obj, LV_OBJ_FLAG_RADIO_BUTTON);
|
||||
|
||||
lv_obj_add_style(obj, &style_radio, LV_PART_INDICATOR);
|
||||
lv_obj_add_style(obj, &style_radio_chk, LV_PART_INDICATOR | LV_STATE_CHECKED);
|
||||
}
|
||||
|
||||
/*Make the first checkbox checked*/
|
||||
lv_obj_add_state(lv_obj_get_child(cont2, 0), LV_STATE_CHECKED);
|
||||
// lv_obj_add_state(lv_obj_get_child(cont, 0), LV_STATE_CHECKED);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user