问题
I am writing an app using JavafxPorts that I am planning on running on both Android and Desktop (PC).
I am trying to keep my code as generic as possible.
My question is... how can I support both the "back" button on Android and have it be equivalent to the "Escape" key on PC?
Basically I want to bring up the menu when the back button is pressed on Android or if the Escape key is pressed on PC.
Thanks!
回答1:
JavaFXPorts already has support for the back button on Android: it is mapped to the Escape key.
You can also use Gluon Charm Down, an OSS project that will let you easily find out about the platform you are running.
public void start(Stage primaryStage) {
Scene = new Scene(...);
scene.addEventHandler(KeyEvent.KEY_RELEASED, e -> {
if (KeyCode.ESCAPE.equals(e.getCode())) {
if (JavaFXPlatform.isAndroid()) {
// bring up the menu or other Android stuff
} else {
// bring up the menu or other Desktop stuff
}
});
}
}
来源:https://stackoverflow.com/questions/36302294/android-back-button-vs-desktop-escape-key