How to output text from textField ?

Guys,

I’m really sorry if the a dumb question. I’ve search big and low but have not been able to find an answer.

I need the user to type in text to the textField and have the typed text appear somewhere in the same scene.

Thanks in advance, Jel [import]uid: 13916 topic_id: 20140 reply_id: 320140[/import]

using the example from here
http://developer.anscamobile.com/reference/index/nativenewtextfield

I modified the snippet like that:

local defaultField  
  
local function fieldHandler( getObj )  
   
 return function( event )  
   
 print( "TextField Object is: " .. tostring( getObj().text ) )  
 redoTxt.text = getObj().text  
  
 if ( "began" == event.phase ) then  
  
 elseif ( "ended" == event.phase ) then  
  
 print( "Text entered = " .. tostring( getObj().text ) ) -- display the text entered  
  
 elseif ( "submitted" == event.phase ) then  
 native.setKeyboardFocus( nil )  
 end  
  
 end   
   
end  
   
-- Create our Text Fields  
defaultField = native.newTextField( 10, 30, 180, 30,  
 fieldHandler( function() return defaultField end ) ) -- passes the text field object  
  
redoTxt = display.newText( "wait...", 10, 150, native.systemFont, 20 )  

-finefin [import]uid: 70635 topic_id: 20140 reply_id: 78688[/import]

Thanks so much for that Canupa. I have a lot to learn =) [import]uid: 13916 topic_id: 20140 reply_id: 78696[/import]

If you only want one field, that example is overly complicated.

local function handlerFunction(event)  
 if event.phase == "submitted" then  
 local enteredText = editField.text  
 native.setKeyboardFocus(nil)  
 end  
end  
  
local editField = native.newTextField( 10, 125, 300, 36, handlerFunction )  
editField.text = "some pre-filled text"  

Basically the object has a .text property that you can either read to get the contents or set to pre-fill the form.
[import]uid: 19626 topic_id: 20140 reply_id: 78717[/import]

Hi Canupa,

If I wanted to add more fields to the above code how would I do it ?

I’ve tried

local defaultField, default2Field

etc but still nothing works. it always prints to the lowest display.newText

Sorry for all the newbie questions

Thanks Jel [import]uid: 13916 topic_id: 20140 reply_id: 78772[/import]

You need to add new x and y coordinates to your new fields or they will all be on top of one another:
local editFieldB = native.newTextField( 10, 225, 300, 36, handlerFunction )

local editFieldC = native.newTextField( 10, 325, 300, 36, handlerFunction )

The first var (10) is the left position
The second var (225 and 325) is the top position [import]uid: 19193 topic_id: 20140 reply_id: 78792[/import]

Hi ScottPhillips,

Thanks for the response. Yep that’s all done but the problem is that typed text from either textField only prints to the second line, or bottom line I only have two lines at the moment.

Jel [import]uid: 13916 topic_id: 20140 reply_id: 78795[/import]

Would you mind posting some example code, should be easy to fix, but hard to envision w/o seeing the code you’re using. [import]uid: 19193 topic_id: 20140 reply_id: 78860[/import]

local defaultField, numberField – forward reference (needed for Lua closure)

– 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() ) )
redoTxt.text = getObj().text

if ( “began” == event.phase ) then
– This is the “keyboard has appeared” event

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

– Hide keyboard
native.setKeyboardFocus( nil )
end

end – “return function()”

end

– Create our Text Field
defaultField = native.newTextField( 10, 30, 180, 30,
fieldHandler( function() return defaultField end ) ) – passes the text field object

redoTxt = display.newText( “text1…”, 10, 140, native.systemFont, 20 )

numberField = native.newTextField( 10, 70, 180, 30,
fieldHandler( function() return numberField end ) )

redoTxt = display.newText( “text2…”, 10, 180, native.systemFont, 20 ) [import]uid: 13916 topic_id: 20140 reply_id: 78887[/import]

Hi ScottPhillips,

Above is the code I’m using. I’m not sure whats the correct way is to post code on the Corona forum at the moment.

Thanks Jel [import]uid: 13916 topic_id: 20140 reply_id: 78888[/import]

 \< code \>  
 your code  
 \< /code \>  
 -- without spaces  

:wink:

-finefin [import]uid: 70635 topic_id: 20140 reply_id: 79414[/import]