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.
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
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]
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?
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?"
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 - 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]
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]