代码之家  ›  专栏  ›  技术社区  ›  Alyona

如何在事件发生后退出EventHandler。消费()?

  •  1
  • Alyona  · 技术社区  · 7 年前

    我的代码是:

    private EventHandler<KeyEvent> fieldChangeListener(final Integer max_Lengh, final Boolean fullTrimming) {
        return new EventHandler<KeyEvent>() {
            @Override
            public void handle(KeyEvent event) {
                TextField field = (TextField) event.getSource();
                String text = field.getText();
                int lastCharIndex = 0;
    
                if (field.getText().length() > 0) {
                    lastCharIndex = field.getText().length() - 1;
                }
    
                if (field.getText().length() >= max_Lengh) {
                    System.out.println("Exit 1");
                    event.consume();
                }
    
                if (fullTrimming) {
                    if (event.getCharacter().matches("\\s")) {
                        System.out.println("Exit 2");
                        event.consume();
                    }
                } else {
                    if (field.getText().length() == 0 && event.getCharacter().matches("\\s")) {
                        System.out.println("Exit 3");
                        event.consume();
                    } else if (field.getText().substring(lastCharIndex).equals(" ")
                            && event.getCharacter().matches("\\s")) {
                        System.out.println("Exit 4");
                        event.consume();
                    }
                }
                Region icon = (Region) ((StackPane)field.getParent()).getChildren().get(1);
                text = text+event.getCharacter();
                System.out.println("Char: " + text);
    

    即使在事件被消耗之后,代码仍会继续执行。因此,我可以得到如下输出:

    Exit 1
    Exit 4
    Char: jk i okkl
    

    EventHandler 一旦 event.consumed()

    1 回复  |  直到 7 年前
        1
  •  0
  •   Tamas Rev    7 年前

    只需添加一些 return

            if (field.getText().length() >= max_Lengh) {
                System.out.println("Exit 1");
                event.consume();
                return;
            }
    
            if (fullTrimming) {
                if (event.getCharacter().matches("\\s")) {
                    System.out.println("Exit 2");
                    event.consume();
                    return;
                }
            } else {
                ...
            }
            ...
    

    然后你可能想重构一下这个方法。通常我们应该只瞄准一个 声明。

    这是代码 全部的 必要的 回来

    public void handle(KeyEvent event) {
                TextField field = (TextField) event.getSource();
                String text = field.getText();
                int lastCharIndex = 0;
    
                if (field.getText().length() > 0) {
                    lastCharIndex = field.getText().length() - 1;
                }
    
                if (field.getText().length() >= max_Lengh) {
                    System.out.println("Exit 1");
                    event.consume();
                    return;
                }
    
                if (fullTrimming) {
                    if (event.getCharacter().matches("\\s")) {
                        System.out.println("Exit 2");
                        event.consume();
                        return;
                    }
                } else {
                    if (field.getText().length() == 0 && event.getCharacter().matches("\\s")) {
                        System.out.println("Exit 3");
                        event.consume();
                        return;
                    } else if (field.getText().substring(lastCharIndex).equals(" ")
                            && event.getCharacter().matches("\\s")) {
                        System.out.println("Exit 4");
                        event.consume();
                        return;
                }
            }
            Region icon = (Region) ((StackPane)field.getParent()).getChildren().get(1);
            text = text+event.getCharacter();
            System.out.println("Char: " + text);
    
            return;
    }