Search code examples
javaandroidlibgdx

Using TextField with LibGDX


I am working on an Android game with LibGDX and would like to implement two TextFields to login to a server.

As far as I know I need to use Stage to add a TextField as an Actor like this:

this.stage = new Stage();
TextField txtf = new TextField("", mSkin);
//...
stage.addActor(txtf);
// And then set the stage as InputProcessor
Gdx.input.setInputProcessor(this.stage);

But I already have a custom InputHandler which implements InputProcessor. I don't really know how I can handle a TextField with my InputHandler.

I have created my TextField in my InputHandler like this:

public class InputHandler implements InputProcessor {

public InputHandler(float ratioWidth, float ratioHeight, GameWorld world) {
        this.world = world;
        this.player = world.getPlayer();
        this.ratioWidth = ratioWidth;
        this.ratioHeight = ratioHeight;

        //...

        this.usernameTextField = new TextField("", AssetLoader.defaultSkin);
        this.usernameTextField.setPosition(24,73);
        this.usernameTextField.setSize(88, 14);
        this.passwordTextField = new TextField("", AssetLoader.defaultSkin);
        this.passwordTextField.setPosition(24, 102);
        this.passwordTextField.setSize(88, 14);

        this.actors.add(this.usernameTextField);
        this.actors.add(this.passwordTextField);
    }

And then I have a method in my InputHandler that can get the TextField for the GameRenderer:

public ArrayList<Actor> getActors() { return this.actors; }

And this is my GameRenderer:

public class GameRenderer {

    public GameRenderer(GameWorld world) {
        this.world = world;

        //...
    }

    //...

    private void drawLogUI() {

        for(Actor a : ((InputHandler) Gdx.input.getInputProcessor()).getActors()){
            a.draw(batcher, 1);
        }

        //...
    }

    public void render(float delta) {

        // Initialisation
        Gdx.gl.glClearColor(32/255.0f, 72/255.0f, 132/255.0f, 1f);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        batcher.begin();
        batcher.enableBlending();

        //...

        drawLogUI();

        //...

        batcher.end();
    }

    //...

}

TextFields are rendering correctly but I can't do anything with them, keyboard doesn't show up, etc. Can someone explain how I can use them in my project ?


Solution

  • You know that you need to use Stage and TextField for your requirement, you created TextField but not adding to stage so you're not using scene2d(stage, actor, textfield..)

    Tutorial that you following, don't have stage so he is using InputProcessor for his requirement.

    TextField usernameTextField = new TextField("", AssetLoader.defaultSkin);
    usernameTextField.setPosition(24,73);
    usernameTextField.setSize(88, 14);
    
    stage.add(usernameTextField);            // <-- Actor now on stage 
    Gdx.input.setInputProcessor(stage);
    

    Inside render() method call

    stage.draw();
    stage.act();