javalibgdxtextfield

Creating a TextField in LibGDX


I am working on an Android game using LibGDX and am trying to make a TextField to take in a player's name.This is what I have so far:

TextField.TextFieldStyle style = new TextField.TextFieldStyle();
    style.font = new BitmapFont();
    style.fontColor = Color.CHARTREUSE;

    TextField field = new TextField("", style);
    field.setText("Test");
    field.setWidth(150);

I then created a table, and added the TextField to it.

table.add(field).expandX().padTop(10);

    stage.addActor(table);

    Gdx.input.setInputProcessor(stage);

And this is what's in my render method:

 Gdx.gl.glClearColor(0, 0, 0, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

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

The only thing that appears is "Test", with nothing underneath. So the TextField is being created, but cannot take in any input. I have had trouble with Skins in the past, and didn't want to use one if I didn't have to. But I don't know if it's my lack of Skins that is the reason why it's not working, or if it is some other problem. Can someone please explain to me how to take in input in a TextField, or what my problem is?


Solution

  • I also had quite a few problems getting text input from users. I didn't find a way to use a TextView. If you want to get input from the user while using LibGDX, here's an easy way to do it:

    Use a TextInputListener. You can read a tutorial on this page. It's not that beautiful looking, but it does the job just fine.

    If you want the have a custom design, you shouldn't use the method above. When you want input from the user, check if a key is pressed (every frame). If so, append the typed character to a String. Render the String in a nice dialog using a BitmapFont.