Class TextField
- java.lang.Object
-
- com.badlogic.gdx.scenes.scene2d.Actor
-
- com.badlogic.gdx.scenes.scene2d.ui.Widget
-
- com.badlogic.gdx.scenes.scene2d.ui.TextField
-
- All Implemented Interfaces:
Disableable,Layout
- Direct Known Subclasses:
TextArea
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.fontandTextField.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
Clipboardinterface. 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.OnscreenKeyboardfor 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 AndroidTextField.OnscreenKeyboardimplementation will bring up the default IME.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classTextField.DefaultOnscreenKeyboardThe defaultTextField.OnscreenKeyboardused by allTextFieldinstances.static interfaceTextField.OnscreenKeyboardAn interface for onscreen keyboards.classTextField.TextFieldClickListenerBasic input listener for the text fieldstatic interfaceTextField.TextFieldFilterInterface for filtering characters entered into the text field.static interfaceTextField.TextFieldListenerInterface for listening to typed characters.static classTextField.TextFieldStyleThe style for a text field, seeTextField.
-
Field Summary
Fields Modifier and Type Field Description protected static charBACKSPACEprotected static charBULLETprotected static charCARRIAGE_RETURNprotected intcursorprotected static charDELETEprotected java.lang.CharSequencedisplayTextprotected floatfontOffsetprotected FloatArrayglyphPositionsprotected booleanhasSelectionstatic floatkeyRepeatInitialTimestatic floatkeyRepeatTimeprotected GlyphLayoutlayoutprotected static charNEWLINEprotected intselectionStartprotected static charTABprotected java.lang.Stringtextprotected floattextHeightprotected floattextOffsetprotected intvisibleTextEndprotected intvisibleTextStartprotected booleanwriteEnters
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidappendText(java.lang.String str)protected voidcalculateOffsets()voidclearSelection()protected booleancontinueCursor(int index, int offset)voidcopy()Copies the contents of this TextField to theClipboardimplementation set on this TextField.protected InputListenercreateInputListener()voidcut()Copies the selected contents of this TextField to theClipboardimplementation set on this TextField, then removes it.voiddraw(Batch batch, float parentAlpha)If this method is overridden, the super method orWidget.validate()should be called to ensure the widget is laid out.protected voiddrawCursor(Drawable cursorPatch, Batch batch, BitmapFont font, float x, float y)protected voiddrawMessageText(Batch batch, BitmapFont font, float x, float y, float maxWidth)protected voiddrawSelection(Drawable selection, Batch batch, BitmapFont font, float x, float y)Draws selection rectangleprotected voiddrawText(Batch batch, BitmapFont font, float x, float y)intgetAlignment()protected DrawablegetBackgroundDrawable()intgetCursorPosition()InputListenergetDefaultInputListener()booleangetFocusTraversal()intgetMaxLength()java.lang.StringgetMessageText()TextField.OnscreenKeyboardgetOnscreenKeyboard()Default is an instance ofTextField.DefaultOnscreenKeyboard.floatgetPrefHeight()floatgetPrefWidth()booleangetProgrammaticChangeEvents()java.lang.StringgetSelection()intgetSelectionStart()TextField.TextFieldStylegetStyle()Returns the text field's style.java.lang.StringgetText()TextField.TextFieldFiltergetTextFieldFilter()protected floatgetTextY(BitmapFont font, Drawable background)protected voidinitialize()booleanisDisabled()booleanisPasswordMode()protected booleanisWordCharacter(char c)protected intletterUnderCursor(float x)protected voidmoveCursor(boolean forward, boolean jump)voidnext(boolean up)Sets thekeyboard focusto the next TextField.voidselectAll()voidsetAlignment(int alignment)Sets text horizontal alignment (left, center or right).voidsetBlinkTime(float blinkTime)voidsetClipboard(Clipboard clipboard)voidsetCursorPosition(int cursorPosition)Sets the cursor position and clears any selection.voidsetDisabled(boolean disabled)voidsetFocusTraversal(boolean focusTraversal)If true (the default), tab/shift+tab will move to the next text field.voidsetMaxLength(int maxLength)voidsetMessageText(java.lang.String messageText)Sets the text that will be drawn in the text field if no text has been entered.voidsetOnlyFontChars(boolean onlyFontChars)When false, text set bysetText(String)may contain characters not in the font, a space will be displayed instead.voidsetOnscreenKeyboard(TextField.OnscreenKeyboard keyboard)voidsetPasswordCharacter(char passwordCharacter)Sets the password character for the text field.voidsetPasswordMode(boolean passwordMode)If true, the text in this text field will be shown as bullet characters.voidsetProgrammaticChangeEvents(boolean programmaticChangeEvents)If false, methods that change the text will not fireChangeListener.ChangeEvent, the event will be fired only when the user changes the text.voidsetSelection(int selectionStart, int selectionEnd)Sets the selected text.voidsetStyle(TextField.TextFieldStyle style)voidsetText(java.lang.String str)voidsetTextFieldFilter(TextField.TextFieldFilter filter)voidsetTextFieldListener(TextField.TextFieldListener listener)protected int[]wordUnderCursor(int at)-
Methods inherited from class com.badlogic.gdx.scenes.scene2d.ui.Widget
getMaxHeight, getMaxWidth, getMinHeight, getMinWidth, invalidate, invalidateHierarchy, layout, needsLayout, pack, setFillParent, setLayoutEnabled, sizeChanged, validate
-
Methods inherited from class com.badlogic.gdx.scenes.scene2d.Actor
act, addAction, addCaptureListener, addListener, ancestorsVisible, ascendantsVisible, clear, clearActions, clearListeners, clipBegin, clipBegin, clipEnd, debug, drawDebug, drawDebugBounds, fire, firstAscendant, getActions, getCaptureListeners, getColor, getDebug, getHeight, getListeners, getName, getOriginX, getOriginY, getParent, getRight, getRotation, getScaleX, getScaleY, getStage, getTop, getTouchable, getUserObject, getWidth, getX, getX, getY, getY, getZIndex, hasActions, hasKeyboardFocus, hasParent, hasScrollFocus, hit, isAscendantOf, isDescendantOf, isTouchable, isTouchFocusListener, isTouchFocusTarget, isVisible, localToActorCoordinates, localToAscendantCoordinates, localToParentCoordinates, localToScreenCoordinates, localToStageCoordinates, moveBy, notify, parentToLocalCoordinates, positionChanged, remove, removeAction, removeCaptureListener, removeListener, rotateBy, rotationChanged, scaleBy, scaleBy, scaleChanged, screenToLocalCoordinates, setBounds, setColor, setColor, setDebug, setHeight, setName, setOrigin, setOrigin, setOriginX, setOriginY, setParent, setPosition, setPosition, setRotation, setScale, setScale, setScaleX, setScaleY, setSize, setStage, setTouchable, setUserObject, setVisible, setWidth, setX, setX, setY, setY, setZIndex, sizeBy, sizeBy, stageToLocalCoordinates, toBack, toFront, toString
-
-
-
-
Field Detail
-
BACKSPACE
protected static final char BACKSPACE
- See Also:
- Constant Field Values
-
CARRIAGE_RETURN
protected static final char CARRIAGE_RETURN
- See Also:
- Constant Field Values
-
NEWLINE
protected static final char NEWLINE
- See Also:
- Constant Field Values
-
TAB
protected static final char TAB
- See Also:
- Constant Field Values
-
DELETE
protected static final char DELETE
- See Also:
- Constant Field Values
-
BULLET
protected static final char BULLET
- See Also:
- Constant Field Values
-
keyRepeatInitialTime
public static float keyRepeatInitialTime
-
keyRepeatTime
public static float keyRepeatTime
-
text
protected java.lang.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 java.lang.CharSequence displayText
-
fontOffset
protected float fontOffset
-
textHeight
protected float textHeight
-
textOffset
protected float textOffset
-
visibleTextStart
protected int visibleTextStart
-
visibleTextEnd
protected int visibleTextEnd
-
-
Method Detail
-
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 bysetText(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)
-
getStyle
public TextField.TextFieldStyle getStyle()
Returns the text field's style. Modifying the returned style may not have an effect untilsetStyle(TextFieldStyle)is called.
-
calculateOffsets
protected void calculateOffsets()
-
draw
public void draw(Batch batch, float parentAlpha)
Description copied from class:WidgetIf this method is overridden, the super method orWidget.validate()should be called to ensure the widget is laid out.
-
getTextY
protected float getTextY(BitmapFont font, @Null Drawable background)
-
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)
-
copy
public void copy()
Copies the contents of this TextField to theClipboardimplementation set on this TextField.
-
cut
public void cut()
Copies the selected contents of this TextField to theClipboardimplementation set on this TextField, then removes it.
-
next
public void next(boolean up)
Sets thekeyboard focusto 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 java.lang.String getMessageText()
- Returns:
- May be null.
-
setMessageText
public void setMessageText(@Null java.lang.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 java.lang.String str)
- Parameters:
str- If null, "" is used.
-
setText
public void setText(@Null java.lang.String str)
- Parameters:
str- If null, "" is used.
-
getText
public java.lang.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 fireChangeListener.ChangeEvent, the event will be fired only when the user changes the text.
-
getProgrammaticChangeEvents
public boolean getProgrammaticChangeEvents()
-
getSelectionStart
public int getSelectionStart()
-
getSelection
public java.lang.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 ofTextField.DefaultOnscreenKeyboard.
-
setOnscreenKeyboard
public void setOnscreenKeyboard(TextField.OnscreenKeyboard keyboard)
-
setClipboard
public void setClipboard(Clipboard clipboard)
-
getPrefWidth
public float getPrefWidth()
- Specified by:
getPrefWidthin interfaceLayout- Overrides:
getPrefWidthin classWidget
-
getPrefHeight
public float getPrefHeight()
- Specified by:
getPrefHeightin interfaceLayout- Overrides:
getPrefHeightin classWidget
-
setAlignment
public void setAlignment(int alignment)
Sets text horizontal alignment (left, center or right).- See Also:
Align
-
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:
setPasswordCharacter(char)
-
isPasswordMode
public boolean isPasswordMode()
-
setPasswordCharacter
public void setPasswordCharacter(char passwordCharacter)
Sets the password character for the text field. The character must be present in theBitmapFont. Default is 149 (bullet).
-
setBlinkTime
public void setBlinkTime(float blinkTime)
-
setDisabled
public void setDisabled(boolean disabled)
- Specified by:
setDisabledin interfaceDisableable
-
isDisabled
public boolean isDisabled()
- Specified by:
isDisabledin interfaceDisableable
-
moveCursor
protected void moveCursor(boolean forward, boolean jump)
-
continueCursor
protected boolean continueCursor(int index, int offset)
-
-