public class TextField extends Widget implements Disableable
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.
Modifier and Type | Class and Description |
---|---|
static class |
TextField.DefaultOnscreenKeyboard
The default
TextField.OnscreenKeyboard used by all TextField instances. |
static interface |
TextField.OnscreenKeyboard
An interface for onscreen keyboards.
|
class |
TextField.TextFieldClickListener
Basic input listener for the text field
|
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 . |
Modifier and Type | Field and Description |
---|---|
protected static char |
BACKSPACE |
protected static char |
BULLET |
protected static char |
CARRIAGE_RETURN |
protected int |
cursor |
protected static char |
DELETE |
protected java.lang.CharSequence |
displayText |
protected float |
fontOffset |
protected FloatArray |
glyphPositions |
protected boolean |
hasSelection |
static float |
keyRepeatInitialTime |
static float |
keyRepeatTime |
protected GlyphLayout |
layout |
protected static char |
NEWLINE |
protected int |
selectionStart |
protected static char |
TAB |
protected java.lang.String |
text |
protected float |
textHeight |
protected float |
textOffset |
protected boolean |
writeEnters |
Constructor and Description |
---|
TextField(java.lang.String text,
Skin skin) |
TextField(java.lang.String text,
Skin skin,
java.lang.String styleName) |
TextField(java.lang.String text,
TextField.TextFieldStyle style) |
Modifier and Type | Method and Description |
---|---|
void |
appendText(java.lang.String str) |
protected void |
calculateOffsets() |
void |
clearSelection() |
protected boolean |
continueCursor(int index,
int offset) |
void |
copy()
Copies the contents of this TextField to the
Clipboard implementation set on this TextField. |
protected InputListener |
createInputListener() |
void |
cut()
Copies the selected contents of this TextField to the
Clipboard implementation set on this TextField, then removes
it. |
void |
draw(Batch batch,
float parentAlpha)
If this method is overridden, the super method or
Widget.validate() should be called to ensure the widget is laid out. |
protected void |
drawCursor(Drawable cursorPatch,
Batch batch,
BitmapFont font,
float x,
float y) |
protected void |
drawMessageText(Batch batch,
BitmapFont font,
float x,
float y,
float maxWidth) |
protected void |
drawSelection(Drawable selection,
Batch batch,
BitmapFont font,
float x,
float y)
Draws selection rectangle
|
protected void |
drawText(Batch batch,
BitmapFont font,
float x,
float y) |
int |
getAlignment() |
protected Drawable |
getBackgroundDrawable() |
int |
getCursorPosition() |
InputListener |
getDefaultInputListener() |
int |
getMaxLength() |
java.lang.String |
getMessageText() |
TextField.OnscreenKeyboard |
getOnscreenKeyboard()
Default is an instance of
TextField.DefaultOnscreenKeyboard . |
float |
getPrefHeight() |
float |
getPrefWidth() |
boolean |
getProgrammaticChangeEvents() |
java.lang.String |
getSelection() |
int |
getSelectionStart() |
TextField.TextFieldStyle |
getStyle()
Returns the text field's style.
|
java.lang.String |
getText() |
TextField.TextFieldFilter |
getTextFieldFilter() |
protected float |
getTextY(BitmapFont font,
Drawable background) |
protected void |
initialize() |
boolean |
isDisabled() |
boolean |
isPasswordMode() |
protected boolean |
isWordCharacter(char c) |
protected int |
letterUnderCursor(float x) |
protected void |
moveCursor(boolean forward,
boolean jump) |
void |
next(boolean up)
Sets the
keyboard focus to the next TextField. |
void |
selectAll() |
void |
setAlignment(int alignment)
Sets text horizontal alignment (left, center or right).
|
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(java.lang.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 |
setProgrammaticChangeEvents(boolean programmaticChangeEvents)
If false, methods that change the text will not fire
ChangeListener.ChangeEvent , the event will be fired only when user changes
the text. |
void |
setSelection(int selectionStart,
int selectionEnd)
Sets the selected text.
|
void |
setStyle(TextField.TextFieldStyle style) |
void |
setText(java.lang.String str) |
void |
setTextFieldFilter(TextField.TextFieldFilter filter) |
void |
setTextFieldListener(TextField.TextFieldListener listener) |
protected int[] |
wordUnderCursor(int at) |
getMaxHeight, getMaxWidth, getMinHeight, getMinWidth, invalidate, invalidateHierarchy, layout, needsLayout, pack, setFillParent, setLayoutEnabled, sizeChanged, validate
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, 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
protected static final char BACKSPACE
protected static final char CARRIAGE_RETURN
protected static final char NEWLINE
protected static final char TAB
protected static final char DELETE
protected static final char BULLET
public static float keyRepeatInitialTime
public static float keyRepeatTime
protected java.lang.String text
protected int cursor
protected int selectionStart
protected boolean hasSelection
protected boolean writeEnters
protected final GlyphLayout layout
protected final FloatArray glyphPositions
protected java.lang.CharSequence displayText
protected float fontOffset
protected float textHeight
protected float textOffset
public TextField(@Null java.lang.String text, TextField.TextFieldStyle style)
protected void initialize()
protected InputListener createInputListener()
protected int letterUnderCursor(float x)
protected boolean isWordCharacter(char c)
protected int[] wordUnderCursor(int at)
public void setMaxLength(int maxLength)
public int getMaxLength()
public void setOnlyFontChars(boolean onlyFontChars)
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.public void setStyle(TextField.TextFieldStyle style)
public TextField.TextFieldStyle getStyle()
setStyle(TextFieldStyle)
is called.protected void calculateOffsets()
public void draw(Batch batch, float parentAlpha)
Widget
Widget.validate()
should be called to ensure the widget is laid out.protected float getTextY(BitmapFont font, @Null Drawable background)
protected void drawSelection(Drawable selection, Batch batch, BitmapFont font, float x, float y)
protected void drawText(Batch batch, BitmapFont font, float x, float y)
protected void drawMessageText(Batch batch, BitmapFont font, float x, float y, float maxWidth)
protected void drawCursor(Drawable cursorPatch, Batch batch, BitmapFont font, float x, float y)
public void copy()
Clipboard
implementation set on this TextField.public void cut()
Clipboard
implementation set on this TextField, then removes
it.public void next(boolean up)
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.up
- If true, the text field with the same or next smallest y coordinate is found, else the next highest.public InputListener getDefaultInputListener()
public void setTextFieldListener(@Null TextField.TextFieldListener listener)
listener
- May be null.public void setTextFieldFilter(@Null TextField.TextFieldFilter filter)
filter
- May be null.@Null public TextField.TextFieldFilter getTextFieldFilter()
public void setFocusTraversal(boolean focusTraversal)
@Null public java.lang.String getMessageText()
public void setMessageText(@Null java.lang.String messageText)
messageText
- may be null.public void appendText(@Null java.lang.String str)
str
- If null, "" is used.public void setText(@Null java.lang.String str)
str
- If null, "" is used.public java.lang.String getText()
public void setProgrammaticChangeEvents(boolean programmaticChangeEvents)
ChangeListener.ChangeEvent
, the event will be fired only when user changes
the text.public boolean getProgrammaticChangeEvents()
public int getSelectionStart()
public java.lang.String getSelection()
public void setSelection(int selectionStart, int selectionEnd)
public void selectAll()
public void clearSelection()
public void setCursorPosition(int cursorPosition)
public int getCursorPosition()
public TextField.OnscreenKeyboard getOnscreenKeyboard()
TextField.DefaultOnscreenKeyboard
.public void setOnscreenKeyboard(TextField.OnscreenKeyboard keyboard)
public void setClipboard(Clipboard clipboard)
public float getPrefWidth()
getPrefWidth
in interface Layout
getPrefWidth
in class Widget
public float getPrefHeight()
getPrefHeight
in interface Layout
getPrefHeight
in class Widget
public void setAlignment(int alignment)
Align
public int getAlignment()
public void setPasswordMode(boolean passwordMode)
setPasswordCharacter(char)
public boolean isPasswordMode()
public void setPasswordCharacter(char passwordCharacter)
BitmapFont
. Default is 149
(bullet).public void setBlinkTime(float blinkTime)
public void setDisabled(boolean disabled)
setDisabled
in interface Disableable
public boolean isDisabled()
isDisabled
in interface Disableable
protected void moveCursor(boolean forward, boolean jump)
protected boolean continueCursor(int index, int offset)