New native.newTextField API example

I am using the newTextField as the sample from the API documentation states. However, I keep receiving the following warning from the console:

WARNING: The ‘listener’ argument to native.newTextField( left, top, width, height [, listener] ) is deprecated. Call the object method o:addEventListener( ‘userInput’, listener ) instead.

So, I re-wrote my code but I cannot make it work. Can you please help me to understand what I am missing (I know it is something stupid):

[code]

local function fieldHandler_input(getObj)
print(“entered here”)
return function (event)
if (“began” == event.phase) then
elseif (“ended” == event.phase) then
elseif (“submitted” == event.phase) then
local path = system.pathForFile( “alex.txt”, system.DocumentsDirectory )
local file = io.open( path, “w+” )
file:write( toString(getObj().text) )
io.close( file )
native.setKeyboardFocus(nil)
end
end
end
–input = native.newTextField( 723, 6, 271, 37, fieldHandler_input(function() return input end ) )
input = native.newTextField( 723, 6, 271, 37 )
input:addEventListener(input,fieldHandler_input)
input.input = “phone”
[/code] [import]uid: 4883 topic_id: 23765 reply_id: 323765[/import]

You need to add the listener like below. Also you don’t need to use the closure anymore to reference the target object.

[lua] local function fieldHandler_input(event)
if (“began” == event.phase) then

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

elseif (“submitted” == event.phase) then
local path = system.pathForFile( “alex.txt”, system.DocumentsDirectory )
local file = io.open( path, “w+” )
file:write( toString(event.target.text) )
io.close( file )
native.setKeyboardFocus(nil)
end
end
end
–input = native.newTextField( 723, 6, 271, 37, fieldHandler_input(function() return input end ) )
input = native.newTextField( 723, 6, 271, 37 )
input:addEventListener( “userInput”, fieldHandler_input)
input.input = “phone” [/lua]

[import]uid: 8872 topic_id: 23765 reply_id: 95623[/import]

Here’s how I do mine…(using your code…)

  
local function fieldHandler( 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.  
  
 elseif ( "ended" == event.phase ) then  
 -- This event is called when the user stops editing a field: for example, when they touch a different field  
  
 elseif ( "submitted" == event.phase ) then  
 -- This event occurs when the user presses the "return" key (if available) on the onscreen keyboard  
 local path = system.pathForFile( "alex.txt", system.DocumentsDirectory )   
 local file = io.open( path, "w+" )   
 file:write( toString(getObj().text) )   
 io.close( file )   
  
 -- Hide keyboard  
 native.setKeyboardFocus( nil )  
 end  
  
end   
  
 input = native.newTextField( 723, 6, 271, 37)  
 input:addEventListener("userInput", fieldHandler)  
 input.inputType = "phone"  
  

Hope that helps,
Croisened
[import]uid: 48203 topic_id: 23765 reply_id: 95626[/import]

Thank you all. Quick question, is there a way to test it in the simulator (at least a print ("entered here") to validate it is working?

Alex [import]uid: 4883 topic_id: 23765 reply_id: 95940[/import]