This post was originally published at Tutorial Treasury: Native text input fields/boxes

Inputting text

Many apps need to gather responsive text input from the user. Examples include getting a player name for a high score or collecting a phone number to send a text message to. In Corona, text is input from the keyboard using these two APIs:

  • native.newTextField() — This creates a single-line text input field, typically used for gathering smaller bits of text (names, passwords, key codes, etc.).
  • native.newTextBox() — This creates a multi-line text input box of an arbitrary width and height, typically used for gathering longer pieces of text content.

Video introduction

These two videos cover native.newTextField() and native.newTextBox():

Tutorials

FAQ: native text input field/box usage

This FAQ addresses several questions related to native text input, including:

  • What is the difference between the “submitted” and “ended” event phases?
  • What happened to the “submitted” phase for native.newTextBox()?
  • How do I dismiss the keyboard when using native text input objects?
  • How can I edit the text in a native.newTextBox()?

Moving native text input fields/boxes

Because native objects are not part of Corona’s OpenGL display hierarchy, they are treated somewhat differently than typical display objects. Fortunately, Corona’s engineering team implemented functionality which allows native.newTextField() and native.newTextBox() objects to be inserted into standard display groups. Please consult this tutorial which describes how to integrate these elements.

New native text input features

Adjusting the font size within native.newTextField() objects can be challenging because different devices have different UI systems/layouts. For instance, Android has more space around its input fields than iOS. However, Corona engineering implemented APIs which make font sizes and scaling more consistent between platforms. Please read this tutorial for details.

Customizing text input

This tutorial shows you how to incorporate text input fields which more closely match the visual style of your app. In specific, it presents a method of creating text fields with a table of parameters, allowing you to easily create stylized input fields.

Inputting text on Windows Phone 8

Windows Phone 8 support is based on CoronaCards which doesn’t support native API calls. So, if you want to gather text input on this platform, you must do it in native code, and this tutorial shows you exactly how.

Further reading

Sample code

Conclusion

While text input using native APIs is distinct from core display objects, they can work nicely together, and a little effort allows you to easily collect information from the user via the keyboard.


Link to article: 

This post was originally published at Tutorial Treasury: Native text input fields/boxes