Hi Everyone,
I’m relatively new to Corona & having an issues with native.newTextField & Keyboard input. The App is a quiz of multiple choice questions and at the end the user is presented with a score / time and text field to input their name. The user can touch the text field and the onscreen keyboard appears they can type in their name and press the return key to save the score to a sql database. The issue I have is if the user doesn’t enter a name and presses enter, the keyboard will disappear and a prompt appears telling to enter their name. Now after getting this prompt they can go ahead and re-enter their name but after pressing enter the App will save the score but hang on the end screen ? This doesn’t happen if they enter their name the first time ? It also hangs if they set focus on the text field & then remove focus using the hide keyboard key ? I don’t understand why it will only return to the main menu if they don’t faff around on the keyboard or enter a blank name ? I’m sure one of you guys has the answer. Here’s the code.
[code]
local function backtomenu()
director:changeScene(“menu”, “downflip”)
end
– TextField Listener
local function fieldHandler( getObj )
– Use Lua closure in order to access the TextField object
return function( event )
– print( "TextField Object is: " … tostring( getObj() ) )
if ( “began” == event.phase ) then
entername.alpha = 0
– This is the “keyboard has appeared” event
function chknum()
defaultField.text = string.sub(defaultField.text,1,15)
end
Runtime:addEventListener(“enterFrame”,chknum)
elseif ( “ended” == event.phase ) then
– This event is called when the user stops editing a field:
– for example, when they touch a different field or keyboard focus goes away
–print( "Text entered = " … tostring( getObj().text ) ) – display the text entered
elseif ( “submitted” == event.phase ) then
– This event occurs when the user presses the “return” key
– (if available) on the onscreen keyboard
if (defaultField.text == ("")) then
entername.alpha = 1
print (“enter name and click restart”);
else
defaultField:removeSelf()
ms = 0
db:exec ([[INSERT INTO leaderboard VALUES (NULL, ‘]]…defaultField.text…[[’,’]]… score…[[’);]]);
tmr5 = timer.performWithDelay (1000, backtomenu, 1);
end
– Hide keyboard
Runtime:removeEventListener(“enterFrame”,chknum)
native.setKeyboardFocus( nil )
end
end – “return function()”
end
– Create our Text Field
defaultField = native.newTextField( 0, 0, 350, 50,
fieldHandler( function() return defaultField end ) )
defaultField.alpha = 0
defaultField.font = native.newFont(“AHDN”, 35 )
defaultField.x = display.contentWidth /2
defaultField.y =475
localGroup:insert(defaultField);
[code]
Thanks
Ben
[import]uid: 125269 topic_id: 28213 reply_id: 328213[/import]