libgdx API

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:
Layout

public class TextField
extends Widget

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

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.
 
Field Summary
 
Fields inherited from class com.badlogic.gdx.scenes.scene2d.Actor
actions, color, height, name, originX, originY, parent, rotation, scaleX, scaleY, stage, touchable, visible, width, x, y
 
Constructor Summary
TextField(Skin skin)
           
TextField(java.lang.String text, Skin skin)
           
TextField(java.lang.String text, java.lang.String messageText, Skin skin)
           
TextField(java.lang.String text, java.lang.String messageText, TextField.TextFieldStyle style)
           
TextField(java.lang.String text, java.lang.String messageText, TextField.TextFieldStyle style, java.lang.String name)
           
TextField(java.lang.String text, TextField.TextFieldStyle style)
           
TextField(TextField.TextFieldStyle style)
           
 
Method Summary
 void clearSelection()
           
 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()
           
 java.lang.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.
 java.lang.String getText()
           
 boolean keyDown(int keycode)
           
 boolean keyTyped(char character)
           
 void layout()
          Computes and caches any information needed for drawing and, if this actor has children, positions and sizes each child and calls Layout.invalidate() and then Layout.validate() on each one.
 void next(boolean up)
          Focuses the next TextField.
 void setClipboard(Clipboard clipboard)
           
 void setCursorPosition(int cursorPosition)
          Sets the cursor position and clears any selection.
 void setMessageText(java.lang.String messageText)
          Sets the text that will be drawn in the text field if no text has been entered.
 void setOnscreenKeyboard(TextField.OnscreenKeyboard keyboard)
           
 void setPasswordCharacter(char passwordCharacter)
           
 void setPasswordMode(boolean passwordMode)
          If true, the text in this text field will be shown as bullet characters.
 void setSelection(int selectionStart, int selectionEnd)
          Sets the selected text.
 void setStyle(TextField.TextFieldStyle style)
           
 void setText(java.lang.String text)
           
 void setTextFieldFilter(TextField.TextFieldFilter filter)
           
 void setTextFieldListener(TextField.TextFieldListener listener)
           
 boolean touchDown(float x, float y, int pointer)
           
 
Methods inherited from class com.badlogic.gdx.scenes.scene2d.ui.Widget
getMaxHeight, getMaxWidth, getMinHeight, getMinWidth, hit, invalidate, invalidateHierarchy, needsLayout, pack, setFillParent, toScreenCoordinates, touchDragged, touchUp, validate
 
Methods inherited from class com.badlogic.gdx.scenes.scene2d.Actor
act, action, clearActions, getStage, isMarkedToRemove, keyUp, markToRemove, remove, scrolled, toLocalCoordinates, toString, touchMoved
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

TextField

public TextField(Skin skin)

TextField

public TextField(java.lang.String text,
                 Skin skin)

TextField

public TextField(java.lang.String text,
                 java.lang.String messageText,
                 Skin skin)
Parameters:
messageText - Text to show when empty. May be null.

TextField

public TextField(TextField.TextFieldStyle style)

TextField

public TextField(java.lang.String text,
                 TextField.TextFieldStyle style)

TextField

public TextField(java.lang.String text,
                 java.lang.String messageText,
                 TextField.TextFieldStyle style)
Parameters:
messageText - Text to show when empty. May be null.

TextField

public TextField(java.lang.String text,
                 java.lang.String messageText,
                 TextField.TextFieldStyle style,
                 java.lang.String name)
Parameters:
messageText - Text to show when empty. May be null.
Method Detail

setStyle

public void setStyle(TextField.TextFieldStyle style)

setPasswordCharacter

public void setPasswordCharacter(char passwordCharacter)

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.


layout

public void layout()
Description copied from interface: Layout
Computes and caches any information needed for drawing and, if this actor has children, positions and sizes each child and calls Layout.invalidate() and then Layout.validate() on each one. Usually this should not be called directly, instead Layout.validate() should be used.

Specified by:
layout in interface Layout
Overrides:
layout in class Widget

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
Parameters:
batch - the spritebatch to render with
parentAlpha - the parent's alpha value.

touchDown

public boolean touchDown(float x,
                         float y,
                         int pointer)
Overrides:
touchDown in class Widget

keyDown

public boolean keyDown(int keycode)
Overrides:
keyDown in class Actor

keyTyped

public boolean keyTyped(char character)
Overrides:
keyTyped in class Actor

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.

getMessageText

public java.lang.String getMessageText()
Returns:
May be null.

setMessageText

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


setText

public void setText(java.lang.String text)

getText

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

setSelection

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


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

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.


libgdx API

Copyright 2010 Mario Zechner (contact@badlogicgames.com), Nathan Sweet (admin@esotericsoftware.com)