New native.newTextField API Question?

I know that Ansca updated their API’s since when I set up my newTextField via the old way I did it, it said this system is outdated. It now tells me that I should use object:addEventListener. Can someone instruct me why this isn’t working?

[lua]function new()
local localGroup = display.newGroup()
local background = display.newImage(“background.png”)
localGroup:insert(background)

local enterUser = display.newEmbossedText(“Enter User Name:”, _W/9 - 15, 95, native.systemFont,50, {0,0,0,200})
enterName = native.newTextField( 50,150,500,60,hello)

local function save_name(text)
sfg = display.newEmbossedText(enterName, _W/9 - 15, _H/2, native.systemFont,50, {0,0,0,200})
asdf =display.newEmbossedText(enterName.text, _W/9 - 15, _H/1.2, native.systemFont,50, {0,0,0,200})
timer.performWithDelay(5000,changeScene,1)
end

local function changeScene()
director:changeScene(“menu”)
end

enterName:addEventListener(“submitted”, save_name)

return localGroup
end[/lua]

What I want to happen is for the text you enter to be displayed on the screen. Can someone please help? Thanks!

Regards,
Jordan Schuetz
Ninja Pig Studios [import]uid: 29181 topic_id: 23225 reply_id: 323225[/import]

What version of Corona are you using? :slight_smile: [import]uid: 52491 topic_id: 23225 reply_id: 92951[/import]

I’m not totally clear on what you’re trying to do precisely but here’s what I see:

  1. asdf is right, sfg should be changed to enterName.text/removed entirely

  2. Make sure you have that “hello” event function, and it should look something like this:

local function hello(event) asdf:setText(enterName.text) end Otherwise the text object won’t be notified to update! Also double check you’re forward declaring the “hello” function properly, because it won’t throw an error if it can’t find the listener function.

  1. What Peach said :slight_smile:

Since we can’t see all the relevant code, it’s hard to say otherwise why it wouldn’t be working.

p.s. You can update to the non-deprecated api like this (and you might as well ^^)

enterName = native.newTextField(50,150,500,60) enterName:addEventListener("userInput", hello) [import]uid: 87138 topic_id: 23225 reply_id: 92977[/import]

Ok let me more clear, all I want to do is this:

  1. User enters text in text box.
  2. When user clicks submit (return key), it saves the string in a table.

How can I make this work? Revarerie or Peach, could you help me out? Also is it possible to have to the text box only except a limited number of characters. Thank you so much for your assistance! I’m using the build 765 I’m pretty sure.

Regards,
Jordan Schuetz
Ninja Pig Studios [import]uid: 29181 topic_id: 23225 reply_id: 93081[/import]

Jordan, without seeing all your code, where you are calling to “hello” - that has been depricated so it should look something like this…

  
local function hello( 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  
  
 -- Hide keyboard  
 native.setKeyboardFocus( nil )  
 end  
  
end   
  
enterName = native.newTextField( 50,150,500,60)  
enterName:addEventListener("userInput", hello)  
  

That should do it.

–Croisened
[import]uid: 48203 topic_id: 23225 reply_id: 93145[/import]

Sure, so here’s roughly what you’ll need for that textbox event function. I’ll call it “onTyped” just for clarity:

local function onTyped(event)  
 if (event.phase == "submitted") then  
 native.setKeyboardFocus(nil);  
 asdf:setText(enterName.text)  
 end  
end  

Now, if you want to physically limit the number of characters you’re allowed to type in:

if (event.phase == "editing") then  
 --Check if the length is greater than your desired maximum  
 if (#enterName.text \> 5) then  
 --Substring the actual textField object to chop one character off the end  
 userField.text = string.sub(userField.text, 1, -2)   
 end  
end  

Alternatively, you can instead allow the user to type as many as they want, and just alert them to the problem when they try to submit:

local function onTyped(event)  
 if (event.phase == "submitted") then  
 if (#enterName.text ==0 or #enterName.text \> 8) then  
 native.showAlert("Invalid Username", "Please enter your 1-8 character user name.", {"OK"});  
 else  
 native.setKeyboardFocus(nil);  
 asdf:setText(enterName.text)  
 -- Perform login function here if necessary  
 end  
 end  
end  

Complete docs for the listener event:
http://developer.anscamobile.com/reference/index/nativenewtextfield [import]uid: 87138 topic_id: 23225 reply_id: 93146[/import]

Thank you guys both! That helped me so much! I greatly appreciate your time, and let me know if you ever need any assistance in other areas!

Regards,
Jordan Schuetz
Ninja Pig Studios [import]uid: 29181 topic_id: 23225 reply_id: 93149[/import]

By the way the “editing command” doesn’t work. Any other ideas on how to solve that issue with the characters, could you implement a Runtime Listener into it?

[lua]enterName = native.newTextField( 50,150,500,60)
–localGroup:insert(enterName)

local function onTyped(event)
if (event.phase == “ended”) then
–Check if the length is greater than your desired maximum
if (enterName.text > 15) then
–Substring the actual textField object to chop one character off the end
enterName.text = string.sub(enterName.text, 1, -2)
end
elseif (event.phase == “submitted”) then
native.setKeyboardFocus(nil)
asdf =display.newEmbossedText(enterName.text, _W/9 - 15, _H/1.2, native.systemFont,50, {0,0,0,200})
asdf:setText(enterName.text)
timer.performWithDelay(5000,changeScene,1)
localGroup:insert(asdf)
end
end

enterName:addEventListener(“userInput”, onTyped)[/lua]

The text does print on the screen though which is awesome!

Regards,
Jordan Schuetz
Ninja Pig Studios [import]uid: 29181 topic_id: 23225 reply_id: 93153[/import]

Both of the ways proposed can work, I’ve used both :slight_smile:

Make sure you don’t forget the # (the lua operator to get the length of a string). Looks like you’re missing that from the code you just posted.

if (#enterName.text \> 15) then  

Here’s a slightly adjusted version of what you posted that should do the trick. Note that you need to check the length every single time the “editing” (not “ended”!) phase occurs. Otherwise if they enter 20 chars it’ll just be subbed down to 19 rather than 15. If you don’t want the constant checking then use the other way I posted with the submitted and/or ended phase.

[code]
local function onTyped(event)
if (event.phase == “editing”) then
–Check if the length is greater than your desired maximum
if (#enterName.text > 15) then
–Substring the actual textField object to chop one character off the end
enterName.text = string.sub(enterName.text, 1, -2)
end
elseif (event.phase == “submitted”) then
native.setKeyboardFocus(nil)
local asdf = display.newEmbossedText(enterName.text, _W/9 - 15, _H/1.2, native.systemFont, 50, {0,0,0,200})
localGroup:insert(asdf)

timer.performWithDelay(5000,changeScene,1)
end
end
[/code] [import]uid: 87138 topic_id: 23225 reply_id: 93157[/import]

Just put in the code exactly as you said, no errors in the Corona simulator, but when I build it for the device it doesn’t work. The API’s must have changed since the build you were using. [import]uid: 29181 topic_id: 23225 reply_id: 93162[/import]

If it isn’t working on-device then it’s a bug with Corona, as it works as expected in the simulator. It sounds like the “editing” phase might not be getting reported. In the mean time, just use the other method I posted, as that only relies on the same “submit” phase being triggered.

You could also try testing on the last stable build and seeing if it works there. [import]uid: 87138 topic_id: 23225 reply_id: 93171[/import]

What simulator are you using? Does the native.newTextField work on Mac? [import]uid: 29181 topic_id: 23225 reply_id: 93173[/import]

Ah yes, that is with the mac simulator.

I just tested on iPad and it seems to work as expected. I’ll see about trying it on Android as well this afternoon, is that what you’re testing was with? [import]uid: 87138 topic_id: 23225 reply_id: 93334[/import]