Boxy Laze Boxy Laze - 1 month ago 6
Java Question

Java - Libgdx : Check user's input in a textfield?

My if..else statement:

if(réponse.getText().equals("")){
réponse.setText("");
} else if(réponse.getText().equals("riviere")){
game.setScreen(new Stage1(game));
music.stop();
}
else if(!réponse.getText().equals("riviere")){
ecrireLeTexte = true;
}


I created a textfield and then straight away check what's in it but I'm afraid the user can't type anything before the textfield check's...

so how to check the user's input after it's typing ?

Answer

You can put your check in your render method so it will be called each frame or set TextFieldListener to your TextField to listen for changes.

réponse.setTextFieldListener(new TextField.TextFieldListener() {
    @Override
    public void keyTyped(TextField textField, char c) {
        //Here you can compare the text
    }
});

Also remember to set your Stage as InputProcessor fe. Gdx.input.setInputProcessor(stage);