Problems getting text from newTextField

I have been attempting to make use of the native.newTextField object.
The problem I am having is that after some text is entered into the field I am unable to access that text to use/display/print. I attempt to access the text for example:

local object = native.newTextField(10,30,180,30,fieldHandler)

local function fieldHandler( event )
if (“submitted” == event.phase) then
print (“Test”)
print (object.text)
native.setKeyboardFocus( nil)
end
end

I do get Test printed but that is it. The keyboard does not hide.
I have tried placing the print(object.text) after the command to hide the keyboard and the keyboard is hidden, but the print statement still does nothing.

I am able to set the initial text in the newTextField with:
object.text = “Text here”
but how do I access the text that is entered into the field?
Am I missing something simple?

Any assistance with this is appreciated…

Thank you [import]uid: 36999 topic_id: 13037 reply_id: 313037[/import]

I don’t know why its not dismissing the keyboard, but if object.text is empty, you’re not going to get that print to do any thing. Perhaps you could try:

print(“object.txt = [” … object.txt … “]”)

and see what you get.

There was a thread about text fields the other day where I pasted in the working code from my game OmniBlaster:

https://developer.anscamobile.com/forum/2011/07/27/keyboard-function

About the only thing I can think of is if your “object” variable is local to some other function and not available when your event handler fires. Also I don’t think the order should bite you, but just to make sure “fieldHandler” is defined before you use it. In other words move the local object = native.textField() to after the event handler function.
[import]uid: 19626 topic_id: 13037 reply_id: 47874[/import]

Thank you robmiracle for your quick reply. I wrote a test as follows:


local ui = require(“ui”)
io.output():setvbuf(‘no’)
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
print (“Hiding the keyboard”)
– Hide keyboard
native.setKeyboardFocus( nil )
local outText = display.newText(defaultField.text,0,0,native.systemFontBold,14)
outText:setTextColor(170, 170, 255, 255)
outText:setReferencePoint(display.CenterLeftReferencePoint)
outText.x = 10
outText.y = 200
fields:insert(outText)
print(“defualtField.text = [” …defaultField.text… “]”)
end

end
local defaultField, numberField, phoneField, urlField, emailField, passwordField
local fields = display.newGroup()

local inputFontSize = 18
local inputFontHeight = 30

defaultField = native.newTextField( 10, 30, 180, 30, fieldHandler )
defaultField.font = native.newFont( native.systemFontBold, inputFontSize )

– Add fields to our new group
fields:insert(defaultField)

local defaultLabel = display.newText( “Default”, 200, 35, native.systemFont, 18 )
defaultLabel:setTextColor( 170, 170, 255, 255 )

– Determine if running on Corona Simulator

local isSimulator = “simulator” == system.getInfo(“environment”)

– Native Text Fields not supported on Simulator

if isSimulator then
msg = display.newText( “Native Text Fields not supported on Simulator!”, 0, 280, “Verdana-Bold”, 12 )
msg.x = display.contentWidth/2 – center title
msg:setTextColor( 255,255,0 )
end


I run the app on the device and enter “Test” in the newTextField.
My output is limited to a message on the console being printed
Hiding the keyboard

The keyboard does hide, but I do not get anything else. I should get text on the screen and a printout on the console, but I do not.

Am I missing something? I just need to get the text [import]uid: 36999 topic_id: 13037 reply_id: 47891[/import]

Hi,

The main problem in your second example appears to be that you’re declaring the native text field after the listener function, so as far as the function is concerned the object doesn’t exist yet. Unfortunately, Corona’s failing silently at that point and not telling you why it’s not working.

I’ve stripped out all the extraneous bits from your second post; this should work for you:

[lua]local defaultField

local function fieldHandler( event )

if ( “submitted” == event.phase ) then
– This event occurs when the user presses the “return” key (if available) on the onscreen keyboard
print (“Hiding the keyboard”)
– Hide keyboard
native.setKeyboardFocus( nil )
local outText = display.newText(defaultField.text,0,0,native.systemFontBold,14)
outText:setTextColor(170, 170, 255, 255)
outText:setReferencePoint(display.CenterLeftReferencePoint)
outText.x = 10
outText.y = 200
print(defaultField.text)
end

end

local inputFontSize = 18
local inputFontHeight = 30

defaultField = native.newTextField( 10, 30, 180, 30, fieldHandler )
defaultField.font = native.newFont( native.systemFontBold, inputFontSize )[/lua]

Notice that the ‘local defaultField’ declaration comes before the function. Also, I removed the code that inserts the native text fields into display groups - since native UI objects in Corona are not part of the regular display list, these don’t actually do anything.

Hope this works for you.

Thanks,
Darren

P.S. The problem with the code in your first post is probably that the listener function isn’t declared before the call to newTextField, so as far as newTextField is concerned, the listener function doesn’t exist. The opposite of what happened the second time around. Just remember that when you’re working with local variables, you need to forward-declare them, or just make sure they’re declared before they’re referenced. [import]uid: 1294 topic_id: 13037 reply_id: 47900[/import]

Yea, forward declarations (or lack of them) creates a lot of head aches.

In my original post, my text field was declared at the top of the file before the call back handler. [import]uid: 19626 topic_id: 13037 reply_id: 47926[/import]

Thanks Ludicrous Software. It took me a few days to get back to this, but thank you for the information. I have my code working now.

I had been using the code from the Corona Samples as a guide on this. It appears that the same mistake was made in the NativeKeyboard2 sample app. [import]uid: 36999 topic_id: 13037 reply_id: 49029[/import]

I tried the code that Ludicrous Software posted, but when I try testing on an Android device nothing happens when I press enter.

I did some testing with the same box and just added a text that output event.phase and the only states I ever heard were “Began” and “Editing”

[import]uid: 59183 topic_id: 13037 reply_id: 49848[/import]

Hi,

Just copied and pasted that code into a main.lua file, built an apk, and installed on both a Nexus One and a Droid 2. It worked fine for me in both instances. Can’t say why it’s not working for you, unfortunately.

Thanks,
Darren [import]uid: 1294 topic_id: 13037 reply_id: 49850[/import]

Thanks for checking the code.

I have built with both SDK 484 and 591(two xp machines). I am running the code on a Droid Incredible (android 2.2). Since this code is confirmed to work I assume the problem is on my side somewhere. If anyone has an idea of how to get this to work or even why it does not work I would be most appreciative. [import]uid: 59183 topic_id: 13037 reply_id: 49855[/import]