Confirm/Submit Text Field

I made a text input that saves to file today, which I just learned how to do. I actually asked about it, and then answered myself here: http://forums.coronalabs.com/topic/54761-text-input-to-file/?p=284789

Long part:

Now that i have it in place, and I can edit all 10 of my fields with whatever message I like, I have come across one small issue.

When I am “Editing” or my cursor is active in a box, and I close my overlay window, then the message for that field does not update.

This effectively means that one of my fields will be left blank, no matter how you add text to them.

I use a Kindle Fire Gen 1 to test my apps, as I do not actually own a phone.  ;)

I do not see a “Enter/Return” button on my keyboard.

Short part:

I would like to be able to “Submit” my text fields when i hit my “Hide Overlay Button”

Here is one input block of code:

 local input1 -- Create text field input1 = native.newTextField( display.contentCenterX, 95, display.contentWidth-50, 30 ) input1.placeholder = myData.msg1 sceneGroup:insert( input1 ) local function textListener1( event ) if ( event.phase == "began" ) then -- user begins editing defaultField print( event.text ) elseif ( event.phase == "ended" or event.phase == "submitted" ) then -- do something with defaultField text print( event.target.text ) myData.msg1 = input1.text input1.placeholder = myData.msg1 elseif ( event.phase == "editing" ) then print( event.newCharacters ) print( event.oldText ) print( event.startPosition ) print( event.text ) end end input1:addEventListener( "userInput", textListener1 )

Thanks!

Tyler

When you close your overlay just grab the current .text values of your input fields.  No need to explicitly trigger the submit event.

Rob

Oooh!!  Great idea, Rob.

That’ll do the trick.  Thanks!

I am back because I am having issues with your suggestion.

When i tried it at first, It was setting all of my messages to nil when i didn’t type into the fields.

So i decided to write a function to see if they were empty before they were saved.

Even after doing so, when I close the scene, It sets all of my messages to nil and nothing displays.

This is the code:

I put it in the hide event, because I was having stack variable issues with my close button and the local text inputs.

-- "scene:hide()" function scene:hide( event ) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then -- Called when the scene is on screen (but is about to go off screen). -- Insert code here to "pause" the scene. -- Example: stop timers, stop animation, stop audio, etc. function onclose () if input1.text ~= nil then myData.msg1 = input1.text else print "nothing to update" end if input2.text ~= nil then myData.msg2 = input2.text else print "nothing to update" end if input3.text ~= nil then myData.msg3 = input3.text else print "nothing to update" end if input4.text ~= nil then myData.msg4 = input4.text else print "nothing to update" end if input5.text ~= nil then myData.msg5 = input5.text else print "nothing to update" end if input6.text ~= nil then myData.msg6 = input6.text else print "nothing to update" end if input7.text ~= nil then myData.msg7 = input7.text else print "nothing to update" end if input8.text ~= nil then myData.msg8 = input8.text else print "nothing to update" end if input9.text ~= nil then myData.msg9 = input9.text else print "nothing to update" end if input10.text ~= nil then myData.msg10 = input10.text else print "nothing to update" end end onclose()

I can’t think of any reasons off the top of my head that would cause this.  Perhaps put in some more print statements.

Rob

I solved it.

I added an updater to the “editing event” for my fields, and it worked like a charm.

Even my last field to be edited is getting the full message saved to file.

It makes sense that it works, because the “ended event” obviously triggers when you are finished editing.

Without a proper confirm button, this method works just fine.

input1 = native.newTextField( display.contentCenterX, 95, display.contentWidth-50, 30 ) input1.placeholder = myData.msg1 sceneGroup:insert( input1 ) local function textListener1( event ) if ( event.phase == "began" ) then -- user begins editing defaultField print( event.text ) elseif ( event.phase == "ended" or event.phase == "submitted" ) then -- do something with defaultField text print( event.target.text ) myData.msg1 = input1.text input1.placeholder = myData.msg1 elseif ( event.phase == "editing" ) then print( event.newCharacters ) print( event.oldText ) print( event.startPosition ) print( event.text ) --ADDED THIS!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! myData.msg1 = input1.text input1.placeholder = myData.msg1 --!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! end end input1:addEventListener( "userInput", textListener1 )

When you close your overlay just grab the current .text values of your input fields.  No need to explicitly trigger the submit event.

Rob

Oooh!!  Great idea, Rob.

That’ll do the trick.  Thanks!

I am back because I am having issues with your suggestion.

When i tried it at first, It was setting all of my messages to nil when i didn’t type into the fields.

So i decided to write a function to see if they were empty before they were saved.

Even after doing so, when I close the scene, It sets all of my messages to nil and nothing displays.

This is the code:

I put it in the hide event, because I was having stack variable issues with my close button and the local text inputs.

-- "scene:hide()" function scene:hide( event ) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then -- Called when the scene is on screen (but is about to go off screen). -- Insert code here to "pause" the scene. -- Example: stop timers, stop animation, stop audio, etc. function onclose () if input1.text ~= nil then myData.msg1 = input1.text else print "nothing to update" end if input2.text ~= nil then myData.msg2 = input2.text else print "nothing to update" end if input3.text ~= nil then myData.msg3 = input3.text else print "nothing to update" end if input4.text ~= nil then myData.msg4 = input4.text else print "nothing to update" end if input5.text ~= nil then myData.msg5 = input5.text else print "nothing to update" end if input6.text ~= nil then myData.msg6 = input6.text else print "nothing to update" end if input7.text ~= nil then myData.msg7 = input7.text else print "nothing to update" end if input8.text ~= nil then myData.msg8 = input8.text else print "nothing to update" end if input9.text ~= nil then myData.msg9 = input9.text else print "nothing to update" end if input10.text ~= nil then myData.msg10 = input10.text else print "nothing to update" end end onclose()

I can’t think of any reasons off the top of my head that would cause this.  Perhaps put in some more print statements.

Rob

I solved it.

I added an updater to the “editing event” for my fields, and it worked like a charm.

Even my last field to be edited is getting the full message saved to file.

It makes sense that it works, because the “ended event” obviously triggers when you are finished editing.

Without a proper confirm button, this method works just fine.

input1 = native.newTextField( display.contentCenterX, 95, display.contentWidth-50, 30 ) input1.placeholder = myData.msg1 sceneGroup:insert( input1 ) local function textListener1( event ) if ( event.phase == "began" ) then -- user begins editing defaultField print( event.text ) elseif ( event.phase == "ended" or event.phase == "submitted" ) then -- do something with defaultField text print( event.target.text ) myData.msg1 = input1.text input1.placeholder = myData.msg1 elseif ( event.phase == "editing" ) then print( event.newCharacters ) print( event.oldText ) print( event.startPosition ) print( event.text ) --ADDED THIS!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! myData.msg1 = input1.text input1.placeholder = myData.msg1 --!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! end end input1:addEventListener( "userInput", textListener1 )