1
votes

I would like to know how to check if a textfield is focus. I have already add a focusListener like this : field.addListener( new FocusListener() {});. But what's next in the if() ?

1
You need to handle the event in your listener by implementing the callback and then set a flag somewhere or execute another action. - noone

1 Answers

2
votes

I wrote this to detect when a libgdx textfield has been focused using either the mouse or the tab key.

<textfield>.addListener(new FocusListener(){
        @Override
        public boolean handle(Event event){
        if (event.toString().equals("mouseMoved") 
                || event.toString().equals("exit") 
                || event.toString().equals("enter") 
                || event.toString().equals("keyDown") 
                || event.toString().equals("touchUp")){

            return false;
        }
            //add your focus handling code here.
            System.out.println("focused");

            return true;
        }

    });

Simply call this method and whenever the textfield is focused, using either the tab key or a click, it will print "focused" to your console.

Feel free to ask for more if needed!

I hope this helps!