EditBox Component Reference

EditBox is a text input component, use this component to get user input easily.

editbox

Click the Add Component button at the bottom of the Inspector panel and select UI/EditBox to add the EditBox component to the node.

To use EditBox, please refer to the EditBox API documentation and the editBox scene of the test-cases-3d project.

EditBox Properties

PropertiesFunction Explanation
BackgroundImageThe Sprite component attached to the node for EditBox’s background
FontColorThe input text color of EditBox
FontSizeThe input text size of EditBox
InputFlagSpecify the input flag: password or capitalized word. (Only supports Android platform)
InputModeSpecify the input mode: multiline or single line
LineHeightThe input text line height of EditBox
MaxLengthThe maximize input characters of EditBox
PlaceholderThe text content of EditBox placeholder
PlaceholderFontColorThe text font color of EditBox placeholder
PlaceholderFontSizeThe text font size of EditBox placeholder
PlaceholderLabelThe Label component attached to the node for EditBox’s placeholder text label
ReturnTypeThe keyboard return type of EditBox. This is useful for keyboard of mobile device
StringThe initial input text of EditBox, which displays the text of the placeholder if not set
TabIndexSet the tabIndex of the DOM input element, only useful on the Web
TextLabelThe Label component attached to the node for EditBox’s input text label

EditBox Events

editbox-event

For event structure you can refer to the Button documentation.

  • Editing Did Began: This event will be triggered when the user clicks on the EditBox.
  • Editing Did Ended: This event will be triggered when the EditBox loses focus.
    • When in single line input mode, it’s triggered after user presses Enter key or clicks the area outside of EditBox.
    • When in multiline input mode, it’s triggered only after user clicks the area outside of EditBox.
  • Text Changed: This event will be triggered when the content in EditBox is changed each time.

Detailed Explanation

  • If you want to input password, you need set Input Flag to PASSWORD and the Input Mode mustn’t be ANY, usually we use Single Line.
  • If you want to enable multiline input support, you can set the Input Mode to Any.
  • The background image of EditBox support 9-slicing sprite frame, you could customize the border as you did in Sprite component.

Add a callback through the script code

Method one

The event callback added by this method is the same as the event callback added by the editor, all added by code. First you need to construct a EventHandler object, and then set the corresponding target, component, handler and customEventData parameters.

  1. import { _decorator, Component, EditBoxComponent, EventHandler } from "cc";
  2. const { ccclass, property } = _decorator;
  3. @ccclass("example")
  4. export class example extends Component {
  5. onLoad() {
  6. const editboxEventHandler = new EventHandler();
  7. // This node is the node to which your event handler code component belongs.
  8. editboxEventHandler.target = this.node;
  9. editboxEventHandler.component = 'example';
  10. editboxEventHandler.handler = 'onEditDidBegan';
  11. editboxEventHandler.customEventData = 'foobar';
  12. const editbox = this.node.getComponent(EditBoxComponent);
  13. editbox.editingDidBegan.push(editboxEventHandler);
  14. // You can also register other callback functions in a similar way.
  15. // editbox.editingDidEnded.push(editboxEventHandler);
  16. // editbox.textChanged.push(editboxEventHandler);
  17. // editbox.editingReturn.push(editboxEventHandler);
  18. }
  19. onEditDidBegan(editbox, customEventData) {
  20. // The editbox here is a cc.EditBox object.
  21. // The customEventData parameter here is equal to the "foobar" you set before.
  22. }
  23. // Suppose this callback is for the editingDidEnded event.
  24. onEditDidEnded(editbox, customEventData) {
  25. // The editbox here is a cc.EditBox object.
  26. // The customEventData parameter here is equal to the "foobar" you set before.
  27. }
  28. // Suppose this callback is for the textChanged event.
  29. onTextChanged(text, editbox, customEventData) {
  30. // The text here indicates the text content of the modified EditBox.
  31. // The editbox here is a cc.EditBox object.
  32. // The customEventData parameter here is equal to the "foobar" you set before.
  33. }
  34. // Suppose this callback is for the editingReturn event.
  35. onEditingReturn(editbox, customEventData) {
  36. // The editbox here is a cc.EditBox object.
  37. // The customEventData parameter here is equal to the "foobar" you set before.
  38. }
  39. }

Method two

Added with Node‘s event API editbox.node.on('editing-did-began', ...).

  1. // Suppose we add an event handler callback inside a component's onLoad method and event handlers in the callback function.
  2. import { _decorator, Component, EditBoxComponent } from "cc";
  3. const { ccclass, property } = _decorator;
  4. @ccclass("example")
  5. export class example extends Component {
  6. @property(EditBoxComponent)
  7. editbox: EditBoxComponent | null = null;
  8. onLoad(){
  9. this.editbox.node.on('editing-did-began', this.callback, this);
  10. }
  11. callback(editbox: EditBoxComponent){
  12. // The callback parameter is the EditBox component, note that events registered this way cannot pass customEventData.
  13. }
  14. }

Similarly, you can register events such as editing-did-ended, text-changed, editing-return, etc. The parameters of the callback function for these events are consistent with the parameters of editing-did-began.