最近刚开始使用 libgdx,我想检查如何让我的键盘输入显示在文本字段上?
谢谢。
如果您正在寻找键盘,那么来自 Kumar 的链接就可以了,如果您想显示要显示的文本,那么您需要使用 InputProcessor LibGDX InputProcessor从键盘获取输入
但是,要从键盘获取输入并仍然对屏幕上的任何其他项目进行输入,您需要将其与 InputMultiplexer LibGDX InputMultiPlexer结合起来,后者可以处理多个 InputProcessors
例如,如果您有一个要处理输入的 Stage,并且您希望键盘经常弹出以进行文本输入,则可以在 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() 事件。每次输入字符时,它都会调用该方法
然后,将两个 InputProcessor 添加到 MultiPlexer,然后将 Gdx.input 设置为 multiPlexer。(Stage 还实现了 InputProcessor...)
private InputMultiplexer multiPlexer;
multiPlexer.addProcessor(keyboardProcessor);
multiPlexer.addProcessor(stage);
Gdx.input.setInputProcessor(multiPlexer);
这允许两个处理器同时工作……多路复用器中的第一个处理器首先处理输入……如果它返回 false,并且另一个 inputProcessor 可以处理它,那么它将被传递。 ..否则它被忽略..
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);