最近刚开始使用libgdx,我想检查如何从键盘输入显示在文本字段上?
感谢。
答案 0 :(得分:1)
如果您正在寻找键盘,那么Kumar的链接就可以了,如果您想要显示文本,那么您需要使用InputProcessor LibGDX InputProcessor来获取键盘的输入
但是,要从键盘获取输入并仍然输入屏幕上的任何其他项目,您需要将其与可以处理多重输入处理器的InputMultiplexer LibGDX InputMultiPlexer相结合
例如,如果您有一个想要处理输入的舞台,并且您希望键盘在文章输入中经常弹出,在Create()中的某个位置,您可以为键盘创建一个新的InputProcessor。
keyboardProcessor = new InputProcessor() {
@Override
public boolean touchUp(int screenX, int screenY, int pointer, int button) {
// TODO Auto-generated method stub
//dLog("This is the new processor");
return false;
}
@Override
public boolean touchDragged(int screenX, int screenY, int pointer) {
// TODO Auto-generated method stub
//dLog("This is the new processor");
return false;
}
@Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
// TODO Auto-generated method stub
//dLog("This is the new processor");
return false;
}
@Override
public boolean scrolled(int amount) {
// TODO Auto-generated method stub
//dLog("This is the new processor");
return false;
}
@Override
public boolean mouseMoved(int screenX, int screenY) {
// TODO Auto-generated method stub
//dLog("This is the new processor");
return false;
}
@Override
public boolean keyUp(int keycode) {
// TODO Auto-generated method stub
//dLog("This is the new processor");
return false;
}
@Override
public boolean keyTyped(char character) {
if(newChatMessage == null){
return false;
}
if((int) character == 10 || (int) character == 13){
dLog("sending in keyTyped");
sendChatMessage();
return false;
}
final String originalChatMessage = newChatMessage;
String tempChatMessage = null;
// dLog("q1");
if((int)character == 8){
// dLog("q2");
if(originalChatMessage.length() > 0 ){
tempChatMessage = originalChatMessage.substring(0, originalChatMessage.length() - 1);
} else {
tempChatMessage = "";
Gdx.app.postRunnable(new Runnable() {
@Override
public void run() {
Assets.soundDoubleBuzzer.play();
}
});
}
} else {
if(originalChatMessage.length() >= 14){
tempChatMessage = originalChatMessage;
//dLog("q3");
Gdx.app.postRunnable(new Runnable() {
@Override
public void run() {
Assets.soundDoubleBuzzer.play();
}
});
} else {
tempChatMessage = originalChatMessage + character;
}
}
// dLog("q4");
final String realTemp = tempChatMessage;
// dLog("q5");
// dLog("user pressed character:" + (int)(character));
// dLog("current message is :" + realTemp);
Gdx.app.postRunnable(new Runnable() {
@Override
public void run() {
lblChat.setText(realTemp);
newChatMessage = realTemp;
}
});
return false;
}
@Override
public boolean keyDown(int keycode) {
// TODO Auto-generated method stub
//dLog("This is the new processor");
return false;
}
};
填写TextField时需要注意的部分是KeyTyped()事件。每次输入一个字符时,它都会调用该方法
然后,将两个InputProcessors添加到MultiPlexer,然后将Gdx.input设置为multiPlexer。 (舞台也实现了一个InputProcessor ......)
private InputMultiplexer multiPlexer;
multiPlexer.addProcessor(keyboardProcessor);
multiPlexer.addProcessor(stage);
Gdx.input.setInputProcessor(multiPlexer);
这允许两个处理器同时运行...多路复用器的第一个进入处理输入的第一个...如果它返回false,而另一个inputProcessor可以处理它,那么它将获得传递...否则被忽略..
答案 1 :(得分:1)
private String txtVal;
TextField textField= new TextField("textField Vallue", skin);
textField.setTextFieldListener(new TextFieldListener() {
@Override
public void keyTyped(TextField textField, char key) {
txtVal= textField.getText();
}
});
System.out.println(txtVal);