问题
Just started to use libgdx recently and i want to check how can i get the input from my keyboard to be displayed on the textfield ?
Thanks.
回答1:
If you are looking for the keyboard, then that link from Kumar will do, if you are trying to get Text to be displayed, then you need to grab the input from the Keyboard with an InputProcessor LibGDX InputProcessor
However, to get input from the keyboard and still have input to any other items on your screen, you need to combine that with an InputMultiplexer LibGDX InputMultiPlexer, which can handle multipe InputProcessors
For example, if you have a Stage that you want to handle input, and you want the Keyboard to popout every so often for text input, somewhere in your Create(), you can create a new InputProcessor for your keyboard..
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;
}
};
The part to pay attention for filling in your TextField is in the KeyTyped() event. Each time a character is typed, it will call that method
Then, you add the two InputProcessors to the MultiPlexer and then set the Gdx.input to the multiPlexer. (the Stage also implements an InputProcessor...)
private InputMultiplexer multiPlexer;
multiPlexer.addProcessor(keyboardProcessor);
multiPlexer.addProcessor(stage);
Gdx.input.setInputProcessor(multiPlexer);
This allows both processors to function at the same time... the first one in to the multiplexer gets first shot at handling the input... if it returns false, and the other inputProcessor can handle it, then it will get passed on... otherwise it is ignored..
回答2:
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);
来源:https://stackoverflow.com/questions/18928295/libgdx-how-to-set-textfield-based-on-inputs-from-keyboard