Problem switching a radio button on/off

local function radioPressed( event )     local radio = event.target     if (radio.isOn) then         radio:setState( {isOn=false} )     else         radio:setState( {isOn=true} )     end     print( tostring( radio ) )end  local radioButton = widget.newSwitch(     {         x = 100,         y = 100,         style = "radio",         id = "RadioButton",         initialSwitchState = true,         onPress = radioPressed     })

The radio starts on, when I press it, it switches off, when I press it again, it stays off when it should go back on. What am I doing wrong?

Also, is There any way to change its color or do I have to use image sheets?

You’re running into one of the paradoxes of this control.  It has three options to handle events:  onPress, onRelease and onEvent.

When you use an onPress event, the value of event.isOn is the value “before” the switch. For onRelease, it’s the value “after” the switch.  For onEvent you will get “begin” and “ended” phases much like a touch event.

This is important because when you use onPress you’re testing the value before the change. 

Also looking at your code, you shouldn’t need to set the value of the switch in the event handler. The switch should change on it’s own. If you need to do additional work, like turning on or off music, then you would do that in the switch handler function. But I would let the switch handle it’s own on/off logic.

Rob

Ok, so where exactly is my problem?

With my current code, when I touch the switch for the first time, it tests for its state before the touch which is ON, then it turns OFF which is what it supposed to do. Then, when I touch it again, the state now is now OFF but, it doesn’t turn back ON.

The way I understand it is that “onPress”  is the right way to go.  because I want to test for its state BEFORE the touch. 

Do you only have one radio button?  If so, change it to style=“checkbox”, and remove all your code around switching the state.

Looks like radio buttons are meant to be used in groups, they aren’t just simply a different style.  So if you have just one of them, it isn’t going to behave like you want.

local function switchPressed( event ) local switch = event.target print("phase = " .. event.phase) print("currently on=" .. tostring(switch.isOn)) end local checkboxSwitch = widget.newSwitch({ x = 100, y = 100, style = "checkbox", initialSwitchState = true, onEvent = switchPressed })

Thanks, it works now. 

You’re running into one of the paradoxes of this control.  It has three options to handle events:  onPress, onRelease and onEvent.

When you use an onPress event, the value of event.isOn is the value “before” the switch. For onRelease, it’s the value “after” the switch.  For onEvent you will get “begin” and “ended” phases much like a touch event.

This is important because when you use onPress you’re testing the value before the change. 

Also looking at your code, you shouldn’t need to set the value of the switch in the event handler. The switch should change on it’s own. If you need to do additional work, like turning on or off music, then you would do that in the switch handler function. But I would let the switch handle it’s own on/off logic.

Rob

Ok, so where exactly is my problem?

With my current code, when I touch the switch for the first time, it tests for its state before the touch which is ON, then it turns OFF which is what it supposed to do. Then, when I touch it again, the state now is now OFF but, it doesn’t turn back ON.

The way I understand it is that “onPress”  is the right way to go.  because I want to test for its state BEFORE the touch. 

Do you only have one radio button?  If so, change it to style=“checkbox”, and remove all your code around switching the state.

Looks like radio buttons are meant to be used in groups, they aren’t just simply a different style.  So if you have just one of them, it isn’t going to behave like you want.

local function switchPressed( event ) local switch = event.target print("phase = " .. event.phase) print("currently on=" .. tostring(switch.isOn)) end local checkboxSwitch = widget.newSwitch({ x = 100, y = 100, style = "checkbox", initialSwitchState = true, onEvent = switchPressed })

Thanks, it works now.