Enable/Disable Radion Button/Checkbox

Hi ,

Could you please tell me how to disable checkbox/radio button? Based user privilege I have to enable/disable few radio button/checkbox. 

Cheers,

Siva

Hi Siva,

At this time, you can turn the widget switch (radio/checkbox) on or off using the :setState() function:

http://docs.coronalabs.com/api/type/SwitchWidget/setState.html

However, this will not “disable” the button when it’s turned off… so you should handle that manually, for example, by setting a variable flag on the switch:

[lua]

myRadioButton.disabled = true

[/lua]

Then, if it’s “disabled”, then when the user taps it, override its turning on in the “onPress” listener, for example:

[lua]

local function onSwitchPress( event )

   local switch = event.target

   if ( switch.disabled == true and switch.isOn == true ) then

      switch:setState( { isOn=false, isAnimated=false } )

   end

end

[/lua]

Take care,

Brent

Hi Brent,

Thanks for your response.

This works perfectly for checkbox & on/Off , but not for radio.  

Cheers,

Siva

Hi Siva,

Can you please post some code that you’re using? This should work for all switches, but I’ll need to see your code.

Thanks,

Brent

Hi Siva,

At this time, you can turn the widget switch (radio/checkbox) on or off using the :setState() function:

http://docs.coronalabs.com/api/type/SwitchWidget/setState.html

However, this will not “disable” the button when it’s turned off… so you should handle that manually, for example, by setting a variable flag on the switch:

[lua]

myRadioButton.disabled = true

[/lua]

Then, if it’s “disabled”, then when the user taps it, override its turning on in the “onPress” listener, for example:

[lua]

local function onSwitchPress( event )

   local switch = event.target

   if ( switch.disabled == true and switch.isOn == true ) then

      switch:setState( { isOn=false, isAnimated=false } )

   end

end

[/lua]

Take care,

Brent

Hi Silva, this work for radio

[lua]

local radio1

local radio2

local function onSwitchPress( event )

    local switch = event.target

    if ( switch.id == “radio1” and switch.isOn == true ) then

        switch:setState( { isOn=false, isAnimated=false } )

        radio2:setState( { isOn=true, isAnimated=false } )

    end

end

radio1 = widget.newSwitch

{

    left = …,

    top = …,

    style = “radio”,

    id = “radio1”,

    initialSwitchState = false,

    onPress = onSwitchPress

}

radio2 = widget.newSwitch

{

    left = …,

    top = …,

    style = “radio”,

    id = “radio2”,

    initialSwitchState = true,

    onPress = onSwitchPress

}

[/lua]

Hi Brent,

Thanks for your response.

This works perfectly for checkbox & on/Off , but not for radio.  

Cheers,

Siva

Hi Siva,

Can you please post some code that you’re using? This should work for all switches, but I’ll need to see your code.

Thanks,

Brent

Hi Silva, this work for radio

[lua]

local radio1

local radio2

local function onSwitchPress( event )

    local switch = event.target

    if ( switch.id == “radio1” and switch.isOn == true ) then

        switch:setState( { isOn=false, isAnimated=false } )

        radio2:setState( { isOn=true, isAnimated=false } )

    end

end

radio1 = widget.newSwitch

{

    left = …,

    top = …,

    style = “radio”,

    id = “radio1”,

    initialSwitchState = false,

    onPress = onSwitchPress

}

radio2 = widget.newSwitch

{

    left = …,

    top = …,

    style = “radio”,

    id = “radio2”,

    initialSwitchState = true,

    onPress = onSwitchPress

}

[/lua]