textfield text value

I’ve got some doubts about nexTextField:

how can I read the .text value?

I am trying this from the listener function but it doens’t seem to work:
if (searchbox.text == “type something”) then…

is there anyway to tweak the borders?

thanks [import]uid: 9304 topic_id: 2159 reply_id: 302159[/import]

OK. got the value.

But I find the phase == “ended” doens’t trigger when I tap another element outside the textfield.

What’s the right way of handling it?

elseif ( “ended” == event.phase ) then
searchbox.text = “ENDED”
native.setKeyboardFocus( nil )

thanks again [import]uid: 9304 topic_id: 2159 reply_id: 6453[/import]

You have to add a touch event outside the text field so when it’s touched the touch listener calls native.setKeyboardFocus(nil). That should fire your text field listener.

-Tom [import]uid: 7559 topic_id: 2159 reply_id: 6576[/import]

Requesting a working example that does what seems like a very common need in my world -

  • a native text input field

  • a button

  • passing the text to a function

The Sample Code has Native Keyboard which demonstrates buttons an native text - but the buttons remove or add the text input fields instead of the more ordinary usage I explain above. And I’ve tried unsuccessfully to make it happen :frowning:

The Sample Code Button Events demonstrates buttons to functions - and I indeed can get my function to run when a button is released! wow the rush of small victories after days of reading and puzzling. It’s like the good feeling after stopping hitting one’s head on the wall.

But that still doesn’t give me a text input that when the button is pressed - passes the text to my function. I’ll keep trying… but I highly advise for the sake of newbies that you make one simple sample that actually does this.

Thanks,

  • April [import]uid: 10016 topic_id: 2159 reply_id: 8115[/import]

PS Also suggest including in the working sample code how to pre-fill the text input field [import]uid: 10016 topic_id: 2159 reply_id: 8117[/import]

Hi April,

just to confirm what you want.

On the screen there is a native text input field plus a button. When do you want the keyboard to appear?
Right away?
That button… when you press it… you want the content of the text input field to be used as a parameter for a function call?

I am asking so I can create a sample for you.

Cheers
Michael Hartlef

http://www.whiteskygames.com
http://www.twitter.com/mhartlef [import]uid: 5712 topic_id: 2159 reply_id: 8150[/import]

Here is come demo code that will do most of what you want. It pre-fills the text field and when the user presses enter, it looks to see if text has changed and displays it in an alert box. I’m sure Mike will come up with a better sample.

local tfield -- forward reference  
  
-- Handle the text field  
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  
  
 -- Hide keyboard  
 native.setKeyboardFocus( nil )  
  
 -- Did the text field change?  
 if tField.text ~= "Name?" then  
 native.showAlert( "Text Field Entered", tField.text )  
 end  
  
 end  
  
end  
  
-- Create Text Field and prefill the field  
--  
tField = native.newTextField( 10, 30, 180, 30, fieldHandler )  
tField.font = native.newFont( native.systemFontBold, 18 )  
tField.text = "Name?"  

-Tom [import]uid: 7559 topic_id: 2159 reply_id: 8267[/import]

Hi Mike,

Keyboard should appear if user taps within the field.

The user may instead if they like the pre-filled content of the field, just press the button.

Yes, the content of the text input field is used as a parameter for a function call.

The above is the way I will use it… but if there is something more generic that all new users would relate to that’s fine. Such as keyboard appears instantly and there’s no prefilled text.

Then maybe a note about how to pre-fill the text. The way Tom shows it looks very easy but being undocumented so far as I can tell… I was not smart enough to just try various things that might work :slight_smile: - or not fully understanding the language yet.

There’s a nice feature in the NativeKeyboard2 example which will make the keyboard disappear if you tap a blank area of the background.

I did finally get my app working by taking NativeKeyboard2 example and then removing a lot of code and stuffing things in. I doubt that I need the ui.lua or some of the other specialized code.

Thanks very much to both Mike and Tom and I hope the examples make it into an area where new users can see it easily.

  • April [import]uid: 10016 topic_id: 2159 reply_id: 8332[/import]

I can’t seem to get this to work. I can’t pass the value. Could anyone check this code?

Thanks

[code]
– Handle the text field
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
jumpscene.stride = numberField.text
– Hide keyboard
native.setKeyboardFocus( nil )

– Did the text field change?
if numberField.text ~= “Name?” then
native.showAlert( “Text Field Entered”, numberField.text )
end

end

end
/[code] [import]uid: 8192 topic_id: 2159 reply_id: 10431[/import]

Is there any way to input real numbers with the number keyboard? [import]uid: 8192 topic_id: 2159 reply_id: 10432[/import]

I entered the above code example and I can’t get my “return” key to work on the iPad 1. Is this a glitch? If not, please help! Thanks. [import]uid: 8780 topic_id: 2159 reply_id: 49319[/import]

I figured out my problem. I was putting the fileHandler(event) below the text field, but I had made it a global function so I don’t understand why it wasn’t working. [import]uid: 8780 topic_id: 2159 reply_id: 49360[/import]