Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Problem switching a radio button on/off
Started by Abdo23 Mar 16 2018 07:19 AM

4 replies to this topic
[TOPIC CONTROLS]
This topic has been archived. This means that you cannot reply to this topic.
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

Abdo23

[GLOBAL: userInfoPane.html]
Abdo23
  • Contributor

  • 120 posts
  • Corona SDK

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?



[TOPIC: post.html]
#2

Rob Miracle

[GLOBAL: userInfoPane.html]
Rob Miracle
  • Moderator

  • 26,544 posts
  • Enterprise

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



[TOPIC: post.html]
#3

Abdo23

[GLOBAL: userInfoPane.html]
Abdo23
  • Contributor

  • 120 posts
  • Corona SDK

 

When you use an onPress event, the value of event.isOn is the value "before" the switch. 

 

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

 

 

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. 



[TOPIC: post.html]
#4

Prairiewest

[GLOBAL: userInfoPane.html]
Prairiewest
  • Observer

  • 26 posts
  • Corona SDK

Ok, so where exactly is my problem?

 

 

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
})


[TOPIC: post.html]
#5

Abdo23

[GLOBAL: userInfoPane.html]
Abdo23
  • Contributor

  • 120 posts
  • Corona SDK

 

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.

 

 

Thanks, it works now. 




[topic_controls]
[/topic_controls]