问题
I have a TextField
, and I want to press the switch charset button programmatically. I've tried this, but I get a NullPointerException
.
myTextfield.addListener(new FocusListener(){
public void keyboardFocusChanged(FocusEvent event, Actor actor, boolean focused){
if(focused){
InputEvent ie = new InputEvent();
ie.setKeyCode(Keys.SWITCH_CHARSET);
actor.fire(ie);
}
}
});
This is the log cat error
回答1:
You need to set the type of the event. That's what causes the NullPointerException
, which you can see if you look at the code (LibGDX is neither closed source, nor black magic). Depending on what you actually want to do, you have to choose another type of event, than the one I chose:
InputEvent ie = new InputEvent();
ie.setKeyCode(Keys.SWITCH_CHARSET);
ie.setType(Type.keyUp);
actor.fire(ie);
来源:https://stackoverflow.com/questions/23221188/how-to-press-a-key-programmatically-in-a-textfield