enable/disable button

How enable/disable button

for example 

resetButton:addEventListener(“tap”, resetTouch)

whether you can get a pointer on this event or disable/enable button? I want temporarily unactive button.

I do not know resetTouch function, because this determined in other lua file (local function).

Perhaps you want to:

resetButton:removeEventListener(“tap”, resetTouch)

when you want the button to not be adtive and only add the event listener when you want it to be active?

If your using widget buttons, they have built in support for this via their isEnabled property, and method object:setEnabled( true/false )

Perhaps you want to:

resetButton:removeEventListener(“tap”, resetTouch)

when you want the button to not be adtive and only add the event listener when you want it to be active?

If your using widget buttons, they have built in support for this via their isEnabled property, and method object:setEnabled( true/false )

hello, im using widget buttoon, but when i call setEnabled(false), the button still do the routine,

and is it will turn into gray if the button is disabled?

and then when i create new button with isDisabled property set to false, the button stil can be touched and execute its event

i m using corona Version 2014.2511 (2014.11.18)

thank you :smiley:

widget.newButton() does not have a .isDisabled property.  It has an isEnabled constructor.  Perhaps if you posted your code, it would help us make sure we are talking about the same thing.

Rob

i m sorry, isEnabled is what i mean

here is my function to create button

function createButton(_param)
    local _button = widget.newButton{
            id=_param.ID,
            isEnabled=true,
            width = _param.W,    height = _param.H,
            defaultFile = _param.DEFAULTFILE,    overFile = _param.OVERFILE,
            label = _param.LABEL, labelAlign = _param.LABELALIGN, labelColor= _param.LABELCOLOR,
            font = _param.FONTNAME, fontSize = _param.FONTSIZE,
            emboss = _param.EMBOSS
        };
    _button.x = _param.X;
    _button.y = _param.Y;
    return _button;
end;

even i put isEnabled = false, the button still can be touch and execute its event listener

like this

function createButton(_param)
    local _button = widget.newButton{
            id=_param.ID,
            isEnabled=true,
            width = _param.W,    height = _param.H,
            defaultFile = _param.DEFAULTFILE,    overFile = _param.OVERFILE,
            label = _param.LABEL, labelAlign = _param.LABELALIGN, labelColor= _param.LABELCOLOR,
            font = _param.FONTNAME, fontSize = _param.FONTSIZE,
            emboss = _param.EMBOSS,
            isEnabled = false
        };
    _button.x = _param.X;
    _button.y = _param.Y;
    return _button;
end;

In your example, you have isEnabled twice.  I’m not sure how Lua handles that.  I did a quick test and isEnable = false does disable the button.

Rob

im sorry for that, here is my complete code

----------------------------------------------------------------------------------------- -- -- main.lua -- ----------------------------------------------------------------------------------------- -- Your code here widget = require( "widget" ); local \_W = display.contentWidth; local \_H = display.contentHeight; \_DEFAULT\_FONT = {NAME = "Calibri", SIZE = 18 }; function createButton(\_param) local \_button = widget.newButton{ id=\_param.ID, isEnabled=false, width = \_param.W, height = \_param.H, defaultFile = \_param.DEFAULTFILE, overFile = \_param.OVERFILE, label = \_param.LABEL, labelAlign = \_param.LABELALIGN, labelColor= \_param.LABELCOLOR, font = \_param.FONTNAME, fontSize = \_param.FONTSIZE, emboss = \_param.EMBOSS }; \_button.x = \_param.X; \_button.y = \_param.Y; return \_button; end; local function buttonTouchEvent( event ) local t = event.target; local stage = display.getCurrentStage(); if t.id=="b1" then if event.phase == "ended" then widgetobj.bLalala2:setEnabled(false); native.showAlert( "Corona", "b1 pressed.", { "OK" }, function() end ) end; elseif t.id=="b2" then if event.phase == "ended" then native.showAlert( "Corona", "b2 pressed.", { "OK" }, function() end ) end; end; end; widgetobj= {}; widgetobj.bLalala1 = createButton( {X = \_W \* 0.85, Y = 0, W = 238, H=44, FONT = \_DEFAULT\_FONT.NAME, FONTSIZE = \_DEFAULT\_FONT.SIZE, DEFAULTFILE = "butt\_mode1.png", OVERFILE = "butt\_mode2.png", LABEL = "b1", ID="b1", LABELALIGN= "center", LABELCOLOR={ default={ 1, 1, 1 }, over={ 0, 0, 0, 0.5 }}, EMBOSS=true} ); widgetobj.bLalala1:addEventListener("touch", buttonTouchEvent); widgetobj.bLalala2 = createButton( {X = \_W \* 0.85, Y = 0, W = 238, H=44, FONT = \_DEFAULT\_FONT.NAME, FONTSIZE = \_DEFAULT\_FONT.SIZE, DEFAULTFILE = "butt\_mode1.png", OVERFILE = "butt\_mode2.png", LABEL = "b2", ID="b2", LABELALIGN= "center", LABELCOLOR={ default={ 1, 1, 1 }, over={ 0, 0, 0, 0.5 }}, EMBOSS=true} ); widgetobj.bLalala2:addEventListener("touch", buttonTouchEvent); widgetobj.bLalala1.x = display.contentCenterX; widgetobj.bLalala1.y = 50 widgetobj.bLalala2.x = display.contentCenterX; widgetobj.bLalala2.y = 100

I see your problem.  You are adding a touch handler to the button which bypasses the buttons’ internal controls.  Instead do this:

function createButton(\_param)     local \_button = widget.newButton{             id=\_param.ID,             isEnabled=false,             width = \_param.W,    height = \_param.H,             defaultFile = \_param.DEFAULTFILE,    overFile = \_param.OVERFILE,             label = \_param.LABEL, labelAlign = \_param.LABELALIGN, labelColor= \_param.LABELCOLOR,             font = \_param.FONTNAME, fontSize = \_param.FONTSIZE,             emboss = \_param.EMBOSS,             onEvent = \_param.EVENTHANDLER,         };     \_button.x = \_param.X;     \_button.y = \_param.Y;     return \_button; end; widgetobj.bLalala1 = createButton(     {X = \_W \* 0.85, Y = 0,    W = 238, H=44,    FONT = \_DEFAULT\_FONT.NAME,    FONTSIZE = \_DEFAULT\_FONT.SIZE,     DEFAULTFILE = "butt\_mode1.png", OVERFILE = "butt\_mode2.png",          LABEL = "b1",  ID="b1", LABELALIGN= "center",     EVENTHANDLER = buttonTouchEvent,     LABELCOLOR={ default={ 1, 1, 1 }, over={ 0, 0, 0, 0.5 }}, EMBOSS=true} );

For isEnabled to work, the widget has to manage the adding and removing of the touch handler.  You don’t do it yourself.

As a small forum administrative point, in the future please use the <> button in the edit bar to post your code so that it’s formatted correctly.

Rob

okay thank you, now it works fine :smiley:

hello, im using widget buttoon, but when i call setEnabled(false), the button still do the routine,

and is it will turn into gray if the button is disabled?

and then when i create new button with isDisabled property set to false, the button stil can be touched and execute its event

i m using corona Version 2014.2511 (2014.11.18)

thank you :smiley:

widget.newButton() does not have a .isDisabled property.  It has an isEnabled constructor.  Perhaps if you posted your code, it would help us make sure we are talking about the same thing.

Rob

i m sorry, isEnabled is what i mean

here is my function to create button

function createButton(_param)
    local _button = widget.newButton{
            id=_param.ID,
            isEnabled=true,
            width = _param.W,    height = _param.H,
            defaultFile = _param.DEFAULTFILE,    overFile = _param.OVERFILE,
            label = _param.LABEL, labelAlign = _param.LABELALIGN, labelColor= _param.LABELCOLOR,
            font = _param.FONTNAME, fontSize = _param.FONTSIZE,
            emboss = _param.EMBOSS
        };
    _button.x = _param.X;
    _button.y = _param.Y;
    return _button;
end;

even i put isEnabled = false, the button still can be touch and execute its event listener

like this

function createButton(_param)
    local _button = widget.newButton{
            id=_param.ID,
            isEnabled=true,
            width = _param.W,    height = _param.H,
            defaultFile = _param.DEFAULTFILE,    overFile = _param.OVERFILE,
            label = _param.LABEL, labelAlign = _param.LABELALIGN, labelColor= _param.LABELCOLOR,
            font = _param.FONTNAME, fontSize = _param.FONTSIZE,
            emboss = _param.EMBOSS,
            isEnabled = false
        };
    _button.x = _param.X;
    _button.y = _param.Y;
    return _button;
end;

In your example, you have isEnabled twice.  I’m not sure how Lua handles that.  I did a quick test and isEnable = false does disable the button.

Rob

im sorry for that, here is my complete code

----------------------------------------------------------------------------------------- -- -- main.lua -- ----------------------------------------------------------------------------------------- -- Your code here widget = require( "widget" ); local \_W = display.contentWidth; local \_H = display.contentHeight; \_DEFAULT\_FONT = {NAME = "Calibri", SIZE = 18 }; function createButton(\_param) local \_button = widget.newButton{ id=\_param.ID, isEnabled=false, width = \_param.W, height = \_param.H, defaultFile = \_param.DEFAULTFILE, overFile = \_param.OVERFILE, label = \_param.LABEL, labelAlign = \_param.LABELALIGN, labelColor= \_param.LABELCOLOR, font = \_param.FONTNAME, fontSize = \_param.FONTSIZE, emboss = \_param.EMBOSS }; \_button.x = \_param.X; \_button.y = \_param.Y; return \_button; end; local function buttonTouchEvent( event ) local t = event.target; local stage = display.getCurrentStage(); if t.id=="b1" then if event.phase == "ended" then widgetobj.bLalala2:setEnabled(false); native.showAlert( "Corona", "b1 pressed.", { "OK" }, function() end ) end; elseif t.id=="b2" then if event.phase == "ended" then native.showAlert( "Corona", "b2 pressed.", { "OK" }, function() end ) end; end; end; widgetobj= {}; widgetobj.bLalala1 = createButton( {X = \_W \* 0.85, Y = 0, W = 238, H=44, FONT = \_DEFAULT\_FONT.NAME, FONTSIZE = \_DEFAULT\_FONT.SIZE, DEFAULTFILE = "butt\_mode1.png", OVERFILE = "butt\_mode2.png", LABEL = "b1", ID="b1", LABELALIGN= "center", LABELCOLOR={ default={ 1, 1, 1 }, over={ 0, 0, 0, 0.5 }}, EMBOSS=true} ); widgetobj.bLalala1:addEventListener("touch", buttonTouchEvent); widgetobj.bLalala2 = createButton( {X = \_W \* 0.85, Y = 0, W = 238, H=44, FONT = \_DEFAULT\_FONT.NAME, FONTSIZE = \_DEFAULT\_FONT.SIZE, DEFAULTFILE = "butt\_mode1.png", OVERFILE = "butt\_mode2.png", LABEL = "b2", ID="b2", LABELALIGN= "center", LABELCOLOR={ default={ 1, 1, 1 }, over={ 0, 0, 0, 0.5 }}, EMBOSS=true} ); widgetobj.bLalala2:addEventListener("touch", buttonTouchEvent); widgetobj.bLalala1.x = display.contentCenterX; widgetobj.bLalala1.y = 50 widgetobj.bLalala2.x = display.contentCenterX; widgetobj.bLalala2.y = 100

I see your problem.  You are adding a touch handler to the button which bypasses the buttons’ internal controls.  Instead do this:

function createButton(\_param) &nbsp;&nbsp; &nbsp;local \_button = widget.newButton{ &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;id=\_param.ID, &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;isEnabled=false, &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;width = \_param.W,&nbsp;&nbsp; &nbsp;height = \_param.H, &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;defaultFile = \_param.DEFAULTFILE,&nbsp;&nbsp; &nbsp;overFile = \_param.OVERFILE, &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;label = \_param.LABEL, labelAlign = \_param.LABELALIGN, labelColor= \_param.LABELCOLOR, &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;font = \_param.FONTNAME, fontSize = \_param.FONTSIZE, &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;emboss = \_param.EMBOSS, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; onEvent = \_param.EVENTHANDLER, &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;}; &nbsp;&nbsp; &nbsp;\_button.x = \_param.X; &nbsp;&nbsp; &nbsp;\_button.y = \_param.Y; &nbsp;&nbsp; &nbsp;return \_button; end; widgetobj.bLalala1 = createButton( &nbsp;&nbsp; &nbsp;{X = \_W \* 0.85, Y = 0,&nbsp;&nbsp; &nbsp;W = 238, H=44,&nbsp;&nbsp; &nbsp;FONT = \_DEFAULT\_FONT.NAME,&nbsp;&nbsp; &nbsp;FONTSIZE = \_DEFAULT\_FONT.SIZE, &nbsp;&nbsp;&nbsp; DEFAULTFILE = "butt\_mode1.png", OVERFILE = "butt\_mode2.png",&nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp;LABEL = "b1",&nbsp; ID="b1", LABELALIGN= "center", &nbsp;&nbsp;&nbsp; EVENTHANDLER = buttonTouchEvent, &nbsp;&nbsp; &nbsp;LABELCOLOR={ default={ 1, 1, 1 }, over={ 0, 0, 0, 0.5 }}, EMBOSS=true} );

For isEnabled to work, the widget has to manage the adding and removing of the touch handler.  You don’t do it yourself.

As a small forum administrative point, in the future please use the <> button in the edit bar to post your code so that it’s formatted correctly.

Rob

okay thank you, now it works fine :smiley: