问题描述
我正在尝试使用TextField
来获取一些用户输入:
I'm trying to use a TextField
to get some user input:
public void render() {
Gdx.gl.glClear(GL11.GL_COLOR_BUFFER_BIT);
Gdx.gl.glClearColor(0, 0, 0, 0);
batch.begin();
batch.end();
stage = new Stage();
Gdx.input.setInputProcessor(stage);
Skin skin = new Skin(Gdx.files.internal("assets/uiskin.json"));
TextButton btnLogin = new TextButton("Click", skin);
btnLogin.setPosition(300, 300);
btnLogin.setSize(300, 60);
btnLogin.addListener(new ClickListener() {
public boolean touchDown(InputEvent e, float x, float y, int point, int button) {
System.out.println(txfUsername.getText());
return false;
}
});
txfUsername = new TextField("", skin);
txfUsername.setPosition(300, 250);
txfUsername.setSize(300, 40);
stage.addActor(txfUsername);
stage.addActor(btnLogin);
stage.act();
stage.draw();
}
我得到的只是一个空白字段.用户无法以任何方式与其进行交互.
All I get is a blank field. The user can't interact with it in any way.
我遵循了此视频中的说明如何使文本字段可编辑?
I followed the instructions in this videoHow do I make the textfield editable?
推荐答案
您在render
方法中使用txfUsername = new TextField ("", skin);
.这样会在每个渲染器上从头开始创建一个新的TextField.
You use txfUsername = new TextField ("", skin);
in your render
method. That creates a new TextField from scratch on each render.
public void render() {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.begin();
// do other rendering ...
batch.end();
Gdx.app.log("MyTextField", txfUsername.getText());
stage.act(Gdx.graphics.getDeltaTime());
stage.draw();
}
在您的类变量中:
private TextButton btnLogin;
TextField txfUsername;
在您的方法show
或create
(不是render
)中:
In your method show
or create
(not render
):
@Override
public void show() {
btnLogin = new TextButton("Click", skin);
btnLogin.setPosition(300, 300);
btnLogin.setSize(300, 60);
btnLogin.addListener(new ClickListener() {
public boolean touchDown(InputEvent e, float x, float y, int point, int button) {
System.out.println(txfUsername.getText());
return false;
}
});
txfUsername = new TextField("", skin);
txfUsername.setPosition(300, 250);
txfUsername.setSize(300, 40);
stage.addActor(txfUsername);
stage.addActor(btnLogin);
}
使用为该字段编写的txfUsername.getText();
我不知道您如何使用GL11,如果是GL10,我可以理解您的故障,如果在GL10中出现错误,则应该更新libgdx,GL10是一个接口,我认为libgdx中的最新版本不是带来了
I do not know how you worked with GL11, if GL10, I could understand a you malfunction, if you get error in GL10, you should update libgdx, GL10 is an interface and I think last versions for in libgdx not bring already
这篇关于无法显示带有文本字段的用户输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!