[Resolved] Trouble with TextField

Hello everyone, new guy here, just purchased a license :slight_smile:

I have been working on a very simple/boring app and have got stuck with a simple TextField. I have searched over the site and found tonnes of examples from people, some that work and some that didn’t.

Unfortunately i cannot remember where the code i found that worked was. However, it only slightly worked. I could enter text into the box with no errors, but the text would never display anywhere on my screen and i had no errors popping up.

I guess what i am asking for, is can someone share a small piece of code to simply display a text input box, and print out the results onto the screen? I spent the whole day yesterday trying to get it to work!

Many thanks :slight_smile: [import]uid: 120612 topic_id: 26636 reply_id: 326636[/import]

hi im new too a lot of times you can find what your looking for in the apis:
http://developer.anscamobile.com/reference/index

here is the link to the textfield api:
http://developer.anscamobile.com/reference/index/nativenewtextfield

if your talking about a text box here is the link to it:
http://developer.anscamobile.com/reference/index/nativenewtextbox [import]uid: 147322 topic_id: 26636 reply_id: 107941[/import]

if thats not what you want sorry i dont know anything else that has to do with it [import]uid: 147322 topic_id: 26636 reply_id: 107943[/import]

Thank you for the suggestions (and i love your avatar!)

I have visited them links before, and checked out the API. It has example code there, but i’ve seen people say it’s overkill so i am just looking for a simplified version that works :slight_smile: [import]uid: 120612 topic_id: 26636 reply_id: 107951[/import]

There is no ā€˜print on the screen’

You create a textField, and you add an event handler to it.
(all the text field examples include that bit)

to make that text appear somewhere else you need to use the display.newText function.

Of course, having added text to the screen once, if you change the text, how do you change the ā€˜printout’?

have a local theText variable.
use theText = display.newText(… stuff)

When you want to change the printout, use

theText:removeSelf()

and then theText = display.newText( … etc) again

[import]uid: 108660 topic_id: 26636 reply_id: 107952[/import]

Ok back again, i found the code i have been trying. It was from Peach Pellen (love her site) over here

http://developer.anscamobile.com/forum/2012/01/07/text-field-input

and the code was

[lua]local function test (event)
if event.phase == ā€œsubmittedā€ then
print (inputField.text)
end
end

inputField = native.newTextField( 20, 30, 280, 30, test)
inputField.font = native.newFont( native.systemFontBold, 18 )[/lua]

So once again, that code works fine, it throws up no errors at all. But when i click away from the input box, or press enter. The inputted text does not show anywhere on my screen.

Phew, thanks! [import]uid: 120612 topic_id: 26636 reply_id: 107953[/import]

print (inputField.text)

does not print on the device or simulator.

It outputs a row of text to the terminal. Do you know where to find that?
[import]uid: 108660 topic_id: 26636 reply_id: 107957[/import]

Good god i’m such an idiot! Of course it does!

I found the terminal and sure enough, the text was showing fine. Now all i need to do is figure out how to make it show in the simulator instead!

Thanks for your help, i can’t believe what a simple mistake it was!

EDIT

Got it! Thanks for your help! i figured out how to display the inputted text now to the screen. I feel like i’ve overcome a huge barrier in my way! I can’t thank you enough.

Just incase anyone else ever gets this problem, here is the code now

[lua]local textToDisplay = display.newText(ā€œHelloā€,0,0,native.systemFont, 26)
local function test (event)
if event.phase == ā€œsubmittedā€ then
textToDisplay.text = inputField.text

–print (inputField.text)
end
end

inputField = native.newTextField( 20, 30, 280, 30, test)
inputField.font = native.newFont( native.systemFontBold, 18 )
textToDisplay.x = 150
textToDisplay.y = 150[/lua] [import]uid: 120612 topic_id: 26636 reply_id: 107959[/import]

Glad to hear you got it sorted [import]uid: 84637 topic_id: 26636 reply_id: 108074[/import]