com.badlogic.gdx.scenes.scene2d.ui
Class TextField

java.lang.Object
  extended by com.badlogic.gdx.scenes.scene2d.Actor
      extended by com.badlogic.gdx.scenes.scene2d.ui.Widget
          extended by com.badlogic.gdx.scenes.scene2d.ui.TextField
All Implemented Interfaces:
Disableable, Layout

public class TextField
extends Widget
implements Disableable

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.

Author:
mzechner, Nathan Sweet

Nested Class Summary
static class TextField.DefaultOnscreenKeyboard
          The default TextField.OnscreenKeyboard used by all TextField instances.
static interface TextField.OnscreenKeyboard
          An interface for onscreen keyboards.
static interface TextField.TextFieldFilter
          Interface for filtering characters entered into the text field.
static interface TextField.TextFieldListener
          Interface for listening to typed characters.
static class TextField.TextFieldStyle
          The style for a text field, see TextField.
 
Constructor Summary
TextField(String text, Skin skin)
           
TextField(String text, Skin skin, String styleName)
           
TextField(String text, TextField.TextFieldStyle style)
           
 
Method Summary
 void clearSelection()
           
 void copy()
          Copies the contents of this TextField to the Clipboard implementation set on this TextField.
 void cut()
          Copies the selected contents of this TextField to the Clipboard implementation set on this TextField, then removes it.
 void draw(SpriteBatch batch, float parentAlpha)
          If this method is overridden, the super method or Widget.validate() should be called to ensure the widget is laid out.
 int getCursorPosition()
           
 int getMaxLength()
           
 String getMessageText()
           
 TextField.OnscreenKeyboard getOnscreenKeyboard()
          Default is an instance of TextField.DefaultOnscreenKeyboard.
 float getPrefHeight()
           
 float getPrefWidth()
           
 TextField.TextFieldStyle getStyle()
          Returns the text field's style.
 String getText()
           
 TextField.TextFieldFilter getTextFieldFilter()
           
 boolean isDisabled()
           
 boolean isPasswordMode()
           
 void next(boolean up)
          Focuses the next TextField.
 void selectAll()
           
 void setBlinkTime(float blinkTime)
           
 void setClipboard(Clipboard clipboard)
           
 void setCursorPosition(int cursorPosition)
          Sets the cursor position and clears any selection.
 void setDisabled(boolean disabled)
           
 void setFocusTraversal(boolean focusTraversal)
          If true (the default), tab/shift+tab will move to the next text field.
 void setMaxLength(int maxLength)
           
 void setMessageText(String messageText)
          Sets the text that will be drawn in the text field if no text has been entered.
 void setOnlyFontChars(boolean onlyFontChars)
          When false, text set by setText(String) may contain characters not in the font, a space will be displayed instead.
 void setOnscreenKeyboard(TextField.OnscreenKeyboard keyboard)
           
 void setPasswordCharacter(char passwordCharacter)
          Sets the password character for the text field.
 void setPasswordMode(boolean passwordMode)
          If true, the text in this text field will be shown as bullet characters.
 void setRightAligned(boolean rightAligned)
           
 void setSelection(int selectionStart, int selectionEnd)
          Sets the selected text.
 void setStyle(TextField.TextFieldStyle style)
           
 void setText(String text)
           
 void setTextFieldFilter(TextField.TextFieldFilter filter)
           
 void setTextFieldListener(TextField.TextFieldListener listener)
           
 
Methods inherited from class com.badlogic.gdx.scenes.scene2d.ui.Widget
getMaxHeight, getMaxWidth, getMinHeight, getMinWidth, invalidate, invalidateHierarchy, layout, needsLayout, pack, setFillParent, setLayoutEnabled, validate
 
Methods inherited from class com.badlogic.gdx.scenes.scene2d.Actor
act, addAction, addCaptureListener, addListener, clear, clearActions, clearListeners, clipBegin, clipBegin, clipEnd, fire, getActions, getCaptureListeners, getColor, getHeight, getListeners, getName, getOriginX, getOriginY, getParent, getRight, getRotation, getScaleX, getScaleY, getStage, getTop, getTouchable, getWidth, getX, getY, getZIndex, hasParent, hit, isAscendantOf, isDescendantOf, isVisible, localToAscendantCoordinates, localToParentCoordinates, localToStageCoordinates, notify, parentToLocalCoordinates, remove, removeAction, removeCaptureListener, removeListener, rotate, scale, scale, screenToLocalCoordinates, setBounds, setColor, setColor, setHeight, setName, setOrigin, setOriginX, setOriginY, setPosition, setRotation, setScale, setScale, setScaleX, setScaleY, setSize, setTouchable, setVisible, setWidth, setX, setY, setZIndex, size, size, stageToLocalCoordinates, toBack, toFront, toString, translate
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

TextField

public TextField(String text,
                 Skin skin)

TextField

public TextField(String text,
                 Skin skin,
                 String styleName)

TextField

public TextField(String text,
                 TextField.TextFieldStyle style)
Method Detail

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)

setPasswordCharacter

public void setPasswordCharacter(char passwordCharacter)
Sets the password character for the text field. The character must be present in the BitmapFont


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.


draw

public void draw(SpriteBatch 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 - Should be multiplied with the actor's alpha, allowing a parent's alpha to affect all children.

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)
Focuses the next TextField. If none is found, the keyboard is hidden. Does nothing if the text field is not in a stage.

Parameters:
up - If true, the TextField with the same or next smallest y coordinate is found, else the next highest.

setTextFieldListener

public void setTextFieldListener(TextField.TextFieldListener listener)
Parameters:
listener - May be null.

setTextFieldFilter

public void setTextFieldFilter(TextField.TextFieldFilter filter)
Parameters:
filter - May be null.

setFocusTraversal

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


getMessageText

public String getMessageText()
Returns:
May be null.

setMessageText

public void setMessageText(String messageText)
Sets the text that will be drawn in the text field if no text has been entered.

Parameters:
messageText - may be null.

setText

public void setText(String text)

getText

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

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

setRightAligned

public void setRightAligned(boolean rightAligned)

setPasswordMode

public void setPasswordMode(boolean passwordMode)
If true, the text in this text field will be shown as bullet characters. The font must have character 149 or this will have no affect.


setBlinkTime

public void setBlinkTime(float blinkTime)

setDisabled

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

isDisabled

public boolean isDisabled()

isPasswordMode

public boolean isPasswordMode()

getTextFieldFilter

public TextField.TextFieldFilter getTextFieldFilter()


Copyright © 2013. All Rights Reserved.