@@ -125,7 +125,7 @@ def defocus_container(self, container):
125125# Used to edit one setting:
126126class SettingActivity (Activity ):
127127
128- active_radio_index = 0 # Track active radio button index
128+ active_radio_index = - 1 # Track active radio button index
129129
130130 # Widgets:
131131 keyboard = None
@@ -168,7 +168,7 @@ def onCreate(self):
168168 self .radio_container .set_width (lv .pct (100 ))
169169 self .radio_container .set_height (lv .SIZE_CONTENT )
170170 self .radio_container .set_flex_flow (lv .FLEX_FLOW .COLUMN )
171- self .radio_container .add_event_cb (self .radio_event_handler , lv .EVENT .CLICKED , None )
171+ self .radio_container .add_event_cb (self .radio_event_handler , lv .EVENT .VALUE_CHANGED , None )
172172 # Create radio buttons and check the right one
173173 self .active_radio_index = - 1 # none
174174 for i , (option_text , option_value ) in enumerate (ui_options ):
@@ -256,19 +256,22 @@ def onStop(self, screen):
256256
257257 def radio_event_handler (self , event ):
258258 print ("radio_event_handler called" )
259- if self .active_radio_index >= 0 :
260- print (f"removing old CHECKED state from child { self .active_radio_index } " )
261- old_cb = self .radio_container .get_child (self .active_radio_index )
262- old_cb .remove_state (lv .STATE .CHECKED )
263- self .active_radio_index = - 1
264- for childnr in range (self .radio_container .get_child_count ()):
265- child = self .radio_container .get_child (childnr )
266- state = child .get_state ()
267- print (f"radio_container child's state: { state } " )
268- if state & lv .STATE .CHECKED : # State can be something like 19 = lv.STATE.HOVERED (16) & lv.STATE.FOCUSED (2) & lv.STATE.CHECKED (1)
269- self .active_radio_index = childnr
270- break
271- print (f"active_radio_index is now { self .active_radio_index } " )
259+ target_obj = event .get_target_obj ()
260+ target_obj_state = target_obj .get_state ()
261+ print (f"target_obj state { target_obj .get_text ()} is { target_obj_state } " )
262+ checked = target_obj_state & lv .STATE .CHECKED
263+ if not checked :
264+ print ("it's not checked, nothing to do!" )
265+ return
266+ else :
267+ new_checked = target_obj .get_index ()
268+ print (f"new_checked: { new_checked } " )
269+ if self .active_radio_index >= 0 :
270+ old_checked = self .radio_container .get_child (self .active_radio_index )
271+ old_checked .remove_state (lv .STATE .CHECKED )
272+ new_checked_obj = self .radio_container .get_child (new_checked )
273+ new_checked_obj .add_state (lv .STATE .CHECKED )
274+ self .active_radio_index = new_checked
272275
273276 def create_radio_button (self , parent , text , index ):
274277 cb = lv .checkbox (parent )
0 commit comments