thetheodor
thetheodor

Reputation: 248

How can I check if a TextField is focused in Libgdx?

I need to check if a textfield is focused, because if it's, then I can hide it.

Upvotes: 2

Views: 1934

Answers (3)

Charanor
Charanor

Reputation: 860

If you just wish to check if an actor is focused or not you could use either Stage#getKeyboardFocus() or Stage#getScrollFocus() which will return the actor that is currently being focused for keyboard or scroll input. In this case the code might look something like this:

if (stage.getKeyboardFocus() == textField) {
    stage.unfocus(textField);
}

Upvotes: 0

Robert P
Robert P

Reputation: 9793

You could use FocusListener and add it to your TextField. William Reeds soulution should also work, but it is also possible to switch to next TextField by calling method next(boolean up) in TextField or by pressing "tab" if you enable this option (setFocusTraversal(boolean focusTraversal)). In this cases the ClickListener won't work. Try this and tell me if it works i am not sure...

Upvotes: 4

William Reed
William Reed

Reputation: 1834

For a TextField in Libgdx to have focus it must be clicked. So a solution to this would be to add a ClickListener to the Text Field.

Here is an example of this.

    TextField text = new TextField("Text", skin);
    text.addListener(new ClickListener(){
        public void clicked(InputEvent e, float x, float y) {
            //perform some action once it is clicked.
        }
    });

Upvotes: 2

Related Questions