Class TextField

All Implemented Interfaces:
Styleable<TextField.TextFieldStyle>, Disableable, Layout
Direct Known Subclasses:
TextArea

public class TextField extends Widget implements Disableable, Styleable<TextField.TextFieldStyle>
A single-line text input field.

The preferred height of a text field is the height of the TextField.TextFieldStyle.font and TextField.TextFieldStyle.background. The preferred width of a text field is 150, a relatively arbitrary size.

The text field will copy the currently selected text when ctrl+c is pressed, and paste any text in the clipboard when ctrl+v is pressed. Clipboard functionality is provided via the Clipboard interface. Currently there are two standard implementations, one for the desktop and one for Android. The Android clipboard is a stub, as copy & pasting on Android is not supported yet.

The text field allows you to specify an TextField.OnscreenKeyboard for displaying a softkeyboard and piping all key events generated by the keyboard to the text field. There are two standard implementations, one for the desktop and one for Android. The desktop keyboard is a stub, as a softkeyboard is not needed on the desktop. The Android TextField.OnscreenKeyboard implementation will bring up the default IME.

  • Field Details

    • BACKSPACE

      protected static final char BACKSPACE
      See Also:
    • CARRIAGE_RETURN

      protected static final char CARRIAGE_RETURN
      See Also:
    • NEWLINE

      protected static final char NEWLINE
      See Also:
    • TAB

      protected static final char TAB
      See Also:
    • DELETE

      protected static final char DELETE
      See Also:
    • BULLET

      protected static final char BULLET
      See Also:
    • keyRepeatInitialTime

      public static float keyRepeatInitialTime
    • keyRepeatTime

      public static float keyRepeatTime
    • text

      protected String text
    • cursor

      protected int cursor
    • selectionStart

      protected int selectionStart
    • hasSelection

      protected boolean hasSelection
    • writeEnters

      protected boolean writeEnters
    • layout

      protected final GlyphLayout layout
    • glyphPositions

      protected final FloatArray glyphPositions
    • displayText

      protected CharSequence displayText
    • fontOffset

      protected float fontOffset
    • textHeight

      protected float textHeight
    • textOffset

      protected float textOffset
    • visibleTextStart

      protected int visibleTextStart
    • visibleTextEnd

      protected int visibleTextEnd
  • Constructor Details

  • Method Details

    • initialize

      protected void initialize()
    • createInputListener

      protected InputListener createInputListener()
    • letterUnderCursor

      protected int letterUnderCursor(float x)
    • isWordCharacter

      protected boolean isWordCharacter(char c)
    • wordUnderCursor

      protected int[] wordUnderCursor(int at)
    • setMaxLength

      public void setMaxLength(int maxLength)
    • getMaxLength

      public int getMaxLength()
    • setOnlyFontChars

      public void setOnlyFontChars(boolean onlyFontChars)
      When false, text set by setText(String) may contain characters not in the font, a space will be displayed instead. When true (the default), characters not in the font are stripped by setText. Characters not in the font are always stripped when typed or pasted.
    • setStyle

      public void setStyle(TextField.TextFieldStyle style)
      Description copied from interface: Styleable
      Set the current style of the actor
      Specified by:
      setStyle in interface Styleable<TextField.TextFieldStyle>
    • getStyle

      public TextField.TextFieldStyle getStyle()
      Returns the text field's style. Modifying the returned style may not have an effect until setStyle(TextFieldStyle) is called.
      Specified by:
      getStyle in interface Styleable<TextField.TextFieldStyle>
    • calculateOffsets

      protected void calculateOffsets()
    • getBackgroundDrawable

      @Null protected Drawable getBackgroundDrawable()
    • draw

      public void draw(Batch batch, float parentAlpha)
      Description copied from class: Widget
      If this method is overridden, the super method or Widget.validate() should be called to ensure the widget is laid out.
      Overrides:
      draw in class Widget
      parentAlpha - The parent alpha, to be multiplied with this actor's alpha, allowing the parent's alpha to affect all children.
    • getTextY

      protected float getTextY(BitmapFont font, @Null Drawable background)
    • drawBackground

      protected void drawBackground(Drawable background, Batch batch, float x, float y, float width, float height)
    • drawSelection

      protected void drawSelection(Drawable selection, Batch batch, BitmapFont font, float x, float y)
      Draws selection rectangle
    • drawText

      protected void drawText(Batch batch, BitmapFont font, float x, float y)
    • drawMessageText

      protected void drawMessageText(Batch batch, BitmapFont font, float x, float y, float maxWidth)
    • drawCursor

      protected void drawCursor(Drawable cursorPatch, Batch batch, BitmapFont font, float x, float y)
    • updateDisplayText

      protected void updateDisplayText()
    • copy

      public void copy()
      Copies the contents of this TextField to the Clipboard implementation set on this TextField.
    • cut

      public void cut()
      Copies the selected contents of this TextField to the Clipboard implementation set on this TextField, then removes it.
    • next

      public void next(boolean up)
      Sets the keyboard focus to the next TextField. If no next text field is found, the onscreen keyboard is hidden. Does nothing if the text field is not in a stage.
      Parameters:
      up - If true, the text field with the same or next smallest y coordinate is found, else the next highest.
    • getDefaultInputListener

      public InputListener getDefaultInputListener()
    • setTextFieldListener

      public void setTextFieldListener(@Null TextField.TextFieldListener listener)
      Parameters:
      listener - May be null.
    • setTextFieldFilter

      public void setTextFieldFilter(@Null TextField.TextFieldFilter filter)
      Parameters:
      filter - May be null.
    • getTextFieldFilter

      @Null public TextField.TextFieldFilter getTextFieldFilter()
    • setFocusTraversal

      public void setFocusTraversal(boolean focusTraversal)
      If true (the default), tab/shift+tab will move to the next text field.
    • getFocusTraversal

      public boolean getFocusTraversal()
    • getMessageText

      @Null public String getMessageText()
      Returns:
      May be null.
    • setMessageText

      public void setMessageText(@Null String messageText)
      Sets the text that will be drawn in the text field if no text has been entered.
      Parameters:
      messageText - may be null.
    • appendText

      public void appendText(@Null String str)
      Parameters:
      str - If null, "" is used.
    • setText

      public void setText(@Null String str)
      Parameters:
      str - If null, "" is used.
    • getText

      public String getText()
      Returns:
      Never null, might be an empty string.
    • setProgrammaticChangeEvents

      public void setProgrammaticChangeEvents(boolean programmaticChangeEvents)
      If false, methods that change the text will not fire ChangeListener.ChangeEvent, the event will be fired only when the user changes the text.
    • getProgrammaticChangeEvents

      public boolean getProgrammaticChangeEvents()
    • getSelectionStart

      public int getSelectionStart()
    • getSelection

      public String getSelection()
    • setSelection

      public void setSelection(int selectionStart, int selectionEnd)
      Sets the selected text.
    • selectAll

      public void selectAll()
    • clearSelection

      public void clearSelection()
    • setCursorPosition

      public void setCursorPosition(int cursorPosition)
      Sets the cursor position and clears any selection.
    • getCursorPosition

      public int getCursorPosition()
    • getOnscreenKeyboard

      public TextField.OnscreenKeyboard getOnscreenKeyboard()
      Default is an instance of TextField.DefaultOnscreenKeyboard.
    • setOnscreenKeyboard

      public void setOnscreenKeyboard(TextField.OnscreenKeyboard keyboard)
    • setClipboard

      public void setClipboard(Clipboard clipboard)
    • getPrefWidth

      public float getPrefWidth()
      Specified by:
      getPrefWidth in interface Layout
      Overrides:
      getPrefWidth in class Widget
    • getPrefHeight

      public float getPrefHeight()
      Specified by:
      getPrefHeight in interface Layout
      Overrides:
      getPrefHeight in class Widget
    • setAlignment

      public void setAlignment(int alignment)
      Sets text horizontal alignment (left, center or right).
      See Also:
    • getAlignment

      public int getAlignment()
    • setPasswordMode

      public void setPasswordMode(boolean passwordMode)
      If true, the text in this text field will be shown as bullet characters.
      See Also:
    • isPasswordMode

      public boolean isPasswordMode()
    • setPasswordCharacter

      public void setPasswordCharacter(char passwordCharacter)
      Sets the password character for the text field. The character must be present in the BitmapFont. Default is 149 (bullet).
    • setBlinkTime

      public void setBlinkTime(float blinkTime)
    • setDisabled

      public void setDisabled(boolean disabled)
      Specified by:
      setDisabled in interface Disableable
    • isDisabled

      public boolean isDisabled()
      Specified by:
      isDisabled in interface Disableable
    • moveCursor

      protected void moveCursor(boolean forward, boolean jump)
    • continueCursor

      protected boolean continueCursor(int index, int offset)