How to make the SoftKeyboard show up again

后端 未结 2 1693
半阙折子戏
半阙折子戏 2020-12-21 15:16

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

2条回答
  •  粉色の甜心
    2020-12-21 15:32

    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();
      }
    }
    

提交回复
热议问题