Create a search box

Hello, this is my code. I want to create a search box to change from scene to scene.

Please I need help.

local usuario = display.newRect(-45, 15, 64, 29)

    usuario:setFillColor(255,255,255)

    usuario.isVisible = true

    group:insert(usuario)

    --Create our Text Field

    local textBox = native.newTextBox(-45, 15, 64, 29)

    textBox.isVisible = true

    textBox.isEditable = true

    textBox.size = 14 --set the character size

    --textBox.text = “This is information in\na Text Box.\n\nYou can now edit this box.”

    group:insert(textBox)

    local buscar = display.newImage(“imagenes/lupa.png”,-45, 15);

    buscar.xScale = 0.145;

    buscar.yScale = 0.145;

    buscar.isVisible = true

    group:insert(buscar)

    local function searchTextSubmitted(searchText)

        print("Search text submitted " … searchText)

        – Hide keyboard

        native.setKeyboardFocus( nil )

        – Hide searchBox

        searchField.alpha = 0

        searchBarActiveImage.alpha = 0

        cancelButton.alpha = 0

        resetSearchButton.alpha = 0

        – Move tableView down by number of pixels specified in tableViewYDelta variable

        transition.to( list, { y= tableViewYDelta, time = 200, delta = “true” } )

        

        searchBarVisible = false

    

    end

    function buscar(searchText)

        if searchText == naranja then

            storyboard.gotoScene( “naranja”, “fade”, 840 )

        end

        if searchText == Naranja then

            storyboard.gotoScene( “naranja”, “fade”, 840 )

        end

    

    end

    function fieldHandler( textField )

        return function( event )

            if ( “began” == event.phase ) then

                – This is the “keyboard has appeared” event

                – In some cases you may want to adjust the interface when the keyboard appears.

                            print(“began”)

                --buscar(textField().text)

                            

            elseif ( “ended” == event.phase ) then

                – This event is called when the user stops editing a field: for example, when they touch a different field

                            print(“ended”)

                buscar(textField().text)

                            

            elseif ( “editing” == event.phase ) then

                            print(“editing”)

                --buscar(textField().text)

                            

            elseif ( “submitted” == event.phase ) then

                – This event occurs when the user presses the “return” key (if available) on the onscreen keyboard

                             print(“submitted”)                       

                             --print( textField().text )

                --searchTextSubmitted(textField().text)

                --buscar(textField().text)

            end

        end

    end

    textBox:addEventListener(“touch”, fieldHandler)

    usuario:addEventListener(“touch”, fieldHandler)

thanks

One thing that stands out as probably not what’s intended:

if searchText == naranja then if searchText == Naranja then

This says you want to compare the value of the variable named  searchText to the variable named naranja in the first one and the variable named Naranja. I suspect you want to compare the value of the string with the contents of “naranja” and the string with the contents of “Naranja”. 

Rob

Hello Rob, thanks for answering, I want to compare with the word “naranja” and “Naranja”, I forget to set the quotes, but still not function the code.

You’re also getting burned by a quirk of these fields.   “event.text” only exists while it’s in the “editing” phase. When you get to ended, you have to access the text with event.target.text

It is ok this code? I changed that

 

            elseif ( “ended” == event.phase ) then

                – This event is called when the user stops editing a field: for example, when they touch a different field

                            print(“ended”)

                buscarchica(event.target.text)

My new code is: (but doesnt work)

 

 

 

 

 

local usuario = display.newRect(-45, 15, 64, 29)

    usuario:setFillColor(255,255,255)

    usuario.isVisible = true

    group:insert(usuario)

 

    --Create our Text Field

    local textBox = native.newTextBox(-45, 15, 64, 29)

    textBox.isVisible = true

    textBox.isEditable = true

    textBox.size = 14 --set the character size

    --textBox.text = “This is information in\na Text Box.\n\nYou can now edit this box.”

    group:insert(textBox)

 

 

    local buscar = display.newImage(“imagenes/lupa.png”,-45, 15);

 

    buscar.xScale = 0.145;

    buscar.yScale = 0.145;

    buscar.isVisible = true

    group:insert(buscar)

 

 

 

 

    local function searchTextSubmitted(searchText)

 

        print("Search text submitted " … searchText)

        – Hide keyboard

        native.setKeyboardFocus( nil )

        – Hide searchBox

        searchField.alpha = 0

        searchBarActiveImage.alpha = 0

        cancelButton.alpha = 0

        resetSearchButton.alpha = 0

 

        – Move tableView down by number of pixels specified in tableViewYDelta variable

        transition.to( list, { y= tableViewYDelta, time = 200, delta = “true” } )

        

        searchBarVisible = false

    

    end

 

 

 

    function buscar(searchText)

 

        if searchText == “naranja” then

 

            storyboard.gotoScene( “naranja”, “fade”, 840 )

 

        end

 

        if searchText == “Naranja” then

 

            storyboard.gotoScene( “naranja”, “fade”, 840 )

 

        end

    

    end

 

 

 

    function fieldHandler( textField )

 

        return function( event )

 

            if ( “began” == event.phase ) then

                – This is the “keyboard has appeared” event

                – In some cases you may want to adjust the interface when the keyboard appears.

                            print(“began”)

                --buscar(event.text)

                            

            elseif ( “ended” == event.phase ) then

                – This event is called when the user stops editing a field: for example, when they touch a different field

                            print(“ended”)

                buscar(event.target.text)

                            

            elseif ( “editing” == event.phase ) then

                            print(“editing”)

                --buscar(event.text)

 

                            

            elseif ( “submitted” == event.phase ) then

                – This event occurs when the user presses the “return” key (if available) on the onscreen keyboard

                             print(“submitted”)                       

                             --print( textField().text )

                --searchTextSubmitted(textField().text)

                --buscar(event.text)

            end

        end

    end

 

    textBox:addEventListener(“touch”, fieldHandler)

    usuario:addEventListener(“touch”, fieldHandler)

You really should format your code. Use the  blue <> button in the edit bar with bold, italic, etc. and paste your code in there.

You also probably should be putting in some print statements to see what values you are getting at various times.  See:

http://docs.coronalabs.com/guide/basics/debugging/index.html

Rob

local usuario = display.newRect(-45, 15, 64, 29) usuario:setFillColor(255,255,255) usuario.isVisible = true group:insert(usuario) --Create our Text Field local textBox = native.newTextBox(-45, 15, 64, 29) textBox.isVisible = true textBox.isEditable = true textBox.size = 14 --set the character size --textBox.text = "This is information in\na Text Box.\n\nYou can now edit this box." group:insert(textBox) local buscar = display.newImage("imagenes/lupa.png",-45, 15); buscar.xScale = 0.145; buscar.yScale = 0.145; buscar.isVisible = true group:insert(buscar) local function searchTextSubmitted(searchText) print("Search text submitted " .. searchText) -- Hide keyboard native.setKeyboardFocus( nil ) -- Hide searchBox searchField.alpha = 0 searchBarActiveImage.alpha = 0 cancelButton.alpha = 0 resetSearchButton.alpha = 0 -- Move tableView down by number of pixels specified in tableViewYDelta variable transition.to( list, { y= tableViewYDelta, time = 200, delta = "true" } ) searchBarVisible = false end function buscar(searchText) if searchText == "naranja" then storyboard.gotoScene( "naranja", "fade", 840 ) end if searchText == "Naranja" then storyboard.gotoScene( "naranja", "fade", 840 ) end end function fieldHandler( textField ) return function( event ) if ( "began" == event.phase ) then -- This is the "keyboard has appeared" event -- In some cases you may want to adjust the interface when the keyboard appears. print("began") buscar(textField().text) elseif ( "ended" == event.phase ) then -- This event is called when the user stops editing a field: for example, when they touch a different field print("ended") buscar(textField().target.text) elseif ( "editing" == event.phase ) then print("editing") buscar(textField().text) elseif ( "submitted" == event.phase ) then -- This event occurs when the user presses the "return" key (if available) on the onscreen keyboard print("submitted") --print( textField().text ) --searchTextSubmitted(textField().text) buscar(textField().text) end end end textBox:addEventListener("touch", fieldHandler) usuario:addEventListener("touch", fieldHandler)

ok thanks i will check the link

In your prints you should print out the values for textfield().text. I honestly think you should be using event.target.text instead.

Rob

One thing that stands out as probably not what’s intended:

if searchText == naranja then if searchText == Naranja then

This says you want to compare the value of the variable named  searchText to the variable named naranja in the first one and the variable named Naranja. I suspect you want to compare the value of the string with the contents of “naranja” and the string with the contents of “Naranja”. 

Rob

Hello Rob, thanks for answering, I want to compare with the word “naranja” and “Naranja”, I forget to set the quotes, but still not function the code.

You’re also getting burned by a quirk of these fields.   “event.text” only exists while it’s in the “editing” phase. When you get to ended, you have to access the text with event.target.text

It is ok this code? I changed that

 

            elseif ( “ended” == event.phase ) then

                – This event is called when the user stops editing a field: for example, when they touch a different field

                            print(“ended”)

                buscarchica(event.target.text)

My new code is: (but doesnt work)

 

 

 

 

 

local usuario = display.newRect(-45, 15, 64, 29)

    usuario:setFillColor(255,255,255)

    usuario.isVisible = true

    group:insert(usuario)

 

    --Create our Text Field

    local textBox = native.newTextBox(-45, 15, 64, 29)

    textBox.isVisible = true

    textBox.isEditable = true

    textBox.size = 14 --set the character size

    --textBox.text = “This is information in\na Text Box.\n\nYou can now edit this box.”

    group:insert(textBox)

 

 

    local buscar = display.newImage(“imagenes/lupa.png”,-45, 15);

 

    buscar.xScale = 0.145;

    buscar.yScale = 0.145;

    buscar.isVisible = true

    group:insert(buscar)

 

 

 

 

    local function searchTextSubmitted(searchText)

 

        print("Search text submitted " … searchText)

        – Hide keyboard

        native.setKeyboardFocus( nil )

        – Hide searchBox

        searchField.alpha = 0

        searchBarActiveImage.alpha = 0

        cancelButton.alpha = 0

        resetSearchButton.alpha = 0

 

        – Move tableView down by number of pixels specified in tableViewYDelta variable

        transition.to( list, { y= tableViewYDelta, time = 200, delta = “true” } )

        

        searchBarVisible = false

    

    end

 

 

 

    function buscar(searchText)

 

        if searchText == “naranja” then

 

            storyboard.gotoScene( “naranja”, “fade”, 840 )

 

        end

 

        if searchText == “Naranja” then

 

            storyboard.gotoScene( “naranja”, “fade”, 840 )

 

        end

    

    end

 

 

 

    function fieldHandler( textField )

 

        return function( event )

 

            if ( “began” == event.phase ) then

                – This is the “keyboard has appeared” event

                – In some cases you may want to adjust the interface when the keyboard appears.

                            print(“began”)

                --buscar(event.text)

                            

            elseif ( “ended” == event.phase ) then

                – This event is called when the user stops editing a field: for example, when they touch a different field

                            print(“ended”)

                buscar(event.target.text)

                            

            elseif ( “editing” == event.phase ) then

                            print(“editing”)

                --buscar(event.text)

 

                            

            elseif ( “submitted” == event.phase ) then

                – This event occurs when the user presses the “return” key (if available) on the onscreen keyboard

                             print(“submitted”)                       

                             --print( textField().text )

                --searchTextSubmitted(textField().text)

                --buscar(event.text)

            end

        end

    end

 

    textBox:addEventListener(“touch”, fieldHandler)

    usuario:addEventListener(“touch”, fieldHandler)

You really should format your code. Use the  blue <> button in the edit bar with bold, italic, etc. and paste your code in there.

You also probably should be putting in some print statements to see what values you are getting at various times.  See:

http://docs.coronalabs.com/guide/basics/debugging/index.html

Rob

local usuario = display.newRect(-45, 15, 64, 29) usuario:setFillColor(255,255,255) usuario.isVisible = true group:insert(usuario) --Create our Text Field local textBox = native.newTextBox(-45, 15, 64, 29) textBox.isVisible = true textBox.isEditable = true textBox.size = 14 --set the character size --textBox.text = "This is information in\na Text Box.\n\nYou can now edit this box." group:insert(textBox) local buscar = display.newImage("imagenes/lupa.png",-45, 15); buscar.xScale = 0.145; buscar.yScale = 0.145; buscar.isVisible = true group:insert(buscar) local function searchTextSubmitted(searchText) print("Search text submitted " .. searchText) -- Hide keyboard native.setKeyboardFocus( nil ) -- Hide searchBox searchField.alpha = 0 searchBarActiveImage.alpha = 0 cancelButton.alpha = 0 resetSearchButton.alpha = 0 -- Move tableView down by number of pixels specified in tableViewYDelta variable transition.to( list, { y= tableViewYDelta, time = 200, delta = "true" } ) searchBarVisible = false end function buscar(searchText) if searchText == "naranja" then storyboard.gotoScene( "naranja", "fade", 840 ) end if searchText == "Naranja" then storyboard.gotoScene( "naranja", "fade", 840 ) end end function fieldHandler( textField ) return function( event ) if ( "began" == event.phase ) then -- This is the "keyboard has appeared" event -- In some cases you may want to adjust the interface when the keyboard appears. print("began") buscar(textField().text) elseif ( "ended" == event.phase ) then -- This event is called when the user stops editing a field: for example, when they touch a different field print("ended") buscar(textField().target.text) elseif ( "editing" == event.phase ) then print("editing") buscar(textField().text) elseif ( "submitted" == event.phase ) then -- This event occurs when the user presses the "return" key (if available) on the onscreen keyboard print("submitted") --print( textField().text ) --searchTextSubmitted(textField().text) buscar(textField().text) end end end textBox:addEventListener("touch", fieldHandler) usuario:addEventListener("touch", fieldHandler)

ok thanks i will check the link

In your prints you should print out the values for textfield().text. I honestly think you should be using event.target.text instead.

Rob