[Resolved] Problem hiding keyboard

I’ve had some problems hiding the keyboard when pressing the return key.

I’m creating a search for a table, on textField press the keyboard comes up, but it doesn’t seem to be triggering the listener function when I press the return keyboard button.

I’m pretty new with corona so I can’t figure out if the layout is wrong or if i’ve made the listener wrong.

Any tips?

[lua]-----------------------------------------------------------------------------------------

– search.lua


local widget = require “widget”
local storyboard = require( “storyboard” )
local scene = storyboard.newScene()

local sbHeight = display.statusBarHeight;
local tbHeight = 44;
local top = sbHeight + tbHeight + 44;

local objectCount = 10;

– Called when the scene’s view does not exist:
function scene:createScene( event )
local group = self.view

local bg = display.newRect(0,0, display.contentWidth, display.contentHeight)

– TABLE LIST –

local list = widget.newTableView{
top = top,
height = 366,
maskFile = “assets/mask-320x366.png”
}

– handles individual row rendering
local function onRowRender( event )
local row = event.row
local rowGroup = event.view
local label = “Row (”
local color = 0

if row.isCategory then
if row.index == 1 then
label = “17/07/2012”
elseif row.index == 3 then
label = “16/07/2012”
elseif row.index == 5 then
label = “10/07/2012”
end
color = 255
row.textObj = display.newRetinaText( rowGroup, label, 0, 0, native.systemFont, 16 )
else
row.textObj = display.newRetinaText( rowGroup, label … row.index … “)”, 0, 0, native.systemFont, 16 )
end

row.textObj:setTextColor( color )
row.textObj:setReferencePoint( display.CenterLeftReferencePoint )
row.textObj.x, row.textObj.y = 20, rowGroup.contentHeight * 0.5
end

– insert rows into list (tableView widget)
for i=1,objectCount do
local isCategory
local rowColor
local rowHeight
local listener = rowListener

if i == 1 or i == 3 or i == 5 then
isCategory = true
rowHeight = 24
rowColor = { 150, 160, 180, 200 }
listener = nil
end

list:insertRow{
height = rowHeight,
rowColor = rowColor,
isCategory = isCategory,
onRender = onRowRender,
listener = listener
}
end

– END OF TABLE LIST –

– all objects must be added to group (e.g. self.view)
group:insert( bg )
group:insert( list )
end

local textField = native.newTextField( 5, top - 36, 300, 28, textField)
textField.font = native.newFont( native.systemFont, 14 )
textField.text = “Search…”

local function searchEntry( event )
if ( “began” == event.phase ) then
if( textField.text == “Search…” ) then
textField.text = “”;
end
textField.text = “”
– Note: this is the “keyboard appearing” event
– In some cases you may want to adjust the interface while the keyboard is open.
elseif ( “submitted” == event.phase ) then
– Automatically tab to password field if user clicks “Return” on iPhone keyboard (convenient!)
if ( textField.text == “” ) then
textField.text = “Search…”;
end
native.setKeyboardFocus( nil )
end
end

– Create a Background touch event

local bkgd = display.newRect( 0, 0, display.contentWidth, display.contentHeight - 50 )
bkgd:setFillColor( 0, 0, 0, 0 ) – set Alpha = 0 so it doesn’t cover up our buttons/fields

– Tapping screen dismisses the keyboard

– Needed for the Number and Phone textFields since there is
– no return key to clear focus.

local listener = function( event )
– Hide keyboard
print(“tap pressed”)
native.setKeyboardFocus( nil )
end

– Called immediately after scene has moved onscreen:
function scene:enterScene( event )
local group = self.view

– do nothing

end

– Called when scene is about to move offscreen:
function scene:exitScene( event )
local group = self.view
–textField:removeEventListener( “searchEntry”, textField )
–textField:removeSelf()
–textField = nil
– INSERT code here (e.g. stop timers, remove listeners, unload sounds, etc.)

end

– If scene’s view is removed, scene:destroyScene() will be called just prior to:
function scene:destroyScene( event )
local group = self.view

– INSERT code here (e.g. remove listeners, remove widgets, save state variables, etc.)

end


– END OF YOUR IMPLEMENTATION

– “createScene” event is dispatched if scene’s view does not exist
scene:addEventListener( “createScene”, scene )

– “enterScene” event is dispatched whenever scene transition has finished
scene:addEventListener( “enterScene”, scene )

– “exitScene” event is dispatched whenever before next scene’s transition begins
scene:addEventListener( “exitScene”, scene )

– “destroyScene” event is dispatched before view is unloaded, which can be
– automatically unloaded in low memory situations, or explicitly via a call to
– storyboard.purgeScene() or storyboard.removeScene().
scene:addEventListener( “destroyScene”, scene )


– Add listener to background for user “tap”
bkgd:addEventListener( “tap”, listener )

textField:addEventListener( “searchEntry”, searchEntry )

return scene[/lua] [import]uid: 163104 topic_id: 28728 reply_id: 328728[/import]

Just wanted to give an update, have solved the problem thanks to this post by amirfl7.

Here’s my updated code for a search box for build 2012.840, hope this helps anyone with a similar problem!

[lua]-----------------------------------------------------------------------------------------

– search.lua


local widget = require “widget”
local storyboard = require( “storyboard” )
local scene = storyboard.newScene()

local sbHeight = display.statusBarHeight;
local tbHeight = 44;
local top = sbHeight + tbHeight + 44;

– Variables to use later
local listener, searchListener, searchBox, bkgd

– Called when the scene’s view does not exist:
function scene:createScene( event )
local group = self.view

local bg = display.newRect(0,0, display.contentWidth, display.contentHeight)

group:insert( bg )
end

– Create a Background touch event

– Called immediately after scene has moved onscreen:
function scene:enterScene( event )
local group = self.view

– Event functions

listener = function( event )
– Hide keyboard –
print(“tap pressed”)
native.setKeyboardFocus( nil )
end

searchListener = function( event )
– print current phase for debugging –
print( event.phase )

if “began” == event.phase then

searchBox.text = “”;

elseif “submitted” == event.phase then

native.setKeyboardFocus( nil )

elseif “ended” == event.phase then

native.setKeyboardFocus( nil )

end
end

– Local Objects

searchBox = native.newTextField( 5, top - 36, 300, 28)
searchBox.font = native.newFont( native.systemFont, 14 )
searchBox.text = “Search…”

bkgd = display.newRect( 0, 0, display.contentWidth, display.contentHeight - 50 )
bkgd:setFillColor( 0, 0, 0, 0 ) – set Alpha = 0 so it doesn’t cover up our buttons/fields

– Event Listeners

searchBox:addEventListener( “userInput”, searchListener )
bkgd:addEventListener( “tap”, listener )

end

– Called when scene is about to move offscreen:
function scene:exitScene( event )
local group = self.view

– Remove display objects

display.remove( searchBox )
display.remove( bkgd )

– Remove event listeners

searchBox:removeEventListener( “userInput”, searchListener )
bkgd:removeEventListener( “tap”, listener )

end

– If scene’s view is removed, scene:destroyScene() will be called just prior to:
function scene:destroyScene( event )
local group = self.view

– INSERT code here (e.g. remove listeners, remove widgets, save state variables, etc.)

end


– END OF YOUR IMPLEMENTATION

– “createScene” event is dispatched if scene’s view does not exist
scene:addEventListener( “createScene”, scene )

– “enterScene” event is dispatched whenever scene transition has finished
scene:addEventListener( “enterScene”, scene )

– “exitScene” event is dispatched whenever before next scene’s transition begins
scene:addEventListener( “exitScene”, scene )

– “destroyScene” event is dispatched before view is unloaded, which can be
– automatically unloaded in low memory situations, or explicitly via a call to
– storyboard.purgeScene() or storyboard.removeScene().
scene:addEventListener( “destroyScene”, scene )


return scene[/lua] [import]uid: 163104 topic_id: 28728 reply_id: 116034[/import]