Type Function Library native.* Return value TextField Revision Current Public Release (2015.2646) Keywords text input, text fields, native text See also native.newTextBox() native.setKeyboardFocus() object.inputType userInput events
Creates a single-line text field for text input.
Native text fields can listen for userInput events (see example).
Native text fields can display optional placeholder text (
nil by default). This can provide a "hint" as to what the user should enter in the field. If set, the placeholder string is displayed using the same style information (except the text color). The placeholder does not appear once actual text has been input into the field and it does not currently participate in determining the size of the text field.
Native text fields are only available in device builds, the Xcode iOS Simulator, and in the OS X Corona Simulator.
Native text fields are not part of the OpenGL canvas and do not obey the display object hierarchy, so they will always appear in front of normal display objects including images, text, and vector objects. However, native text fields can be "inserted" (object:insert()) into Corona [display groups][api.type.GroupObject.index] and they will move in unison with that group.
Presentation of native controls in the Corona Simulator can only be an approximation of the actual device. Always test on a device and assume that the behavior/appearance on devices is correct.
To remove a native text field from the display, use
For multi-line text input, see native.newTextBox().
The native.setKeyboardFocus() API is used to show and hide the keyboard used for text input (applies to device and Xcode iOS Simulator only).
On Android, due to the fact that Corona runs on multiple threads, getting the
.text value of a TextField immediately after setting it will not return the correct value. This action should be performed following a short timer or during the next time step.
native.newTextField( centerX, centerY, width, height )
Numbers. The x and y coordinates that correspond to the center of the text field.
local defaultField local function textListener( 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 ) elseif ( event.phase == "editing" ) then print( event.newCharacters ) print( event.oldText ) print( event.startPosition ) print( event.text ) end end -- Create text field defaultField = native.newTextField( 150, 150, 180, 30 ) defaultField:addEventListener( "userInput", textListener )
local function handlerFunction( event ) if ( event.phase == "began" ) then -- user begins editing numericField print( event.text ) end end local numericField = native.newTextField( 150, 150, 220, 36 ) numericField.inputType = "number" numericField:addEventListener( "userInput", handlerFunction )