When you hide the SoftKeyboard
via the Android Back Button
, touching the (still focused) inputNode won\'t make the keyboard show again.
To solve th
We improved the initial version of yours with adding a listener for KeyEvents and tested it successfully on Android and iOS.
The defocus-method can easily be called at the start of a view, so that it won't automatically focus the first textfield with "Platform.runlater(() -> textField.defocus());".
public class RefocusableTextField extends TextField {
private Region fakeFocusTarget;
public RefocusableTextField(String text) {
this();
setText(text);
}
public RefocusableTextField() {
fakeFocusTarget = new Region();
fakeFocusTarget.setManaged(false);
getChildren().add(fakeFocusTarget);
addEventFilter(MouseEvent.MOUSE_PRESSED, MouseEvent::consume);
addEventHandler(MouseEvent.MOUSE_CLICKED, e -> {
if (!isFocused()) {
requestFocus();
} else {
defocus();
}
});
addEventHandler(KeyEvent.KEY_PRESSED, e -> {
System.out.println(e.getCode());
if (e.getCode().equals(KeyCode.ENTER)) {
defocus();
}
});
}
public void defocus() {
fakeFocusTarget.requestFocus();
}
}