JavaFX TextField - 只允许输入一个字母

JavaFX TextField - Allow only one letter to be typed

我正在尝试用 JavaFX 制作数独游戏,但我不知道如何只允许输入一个字母。答案是调用文本字段并执行以下操作:

myTextField.setOnKeyPressed(e ->
{
    if (!myTextField.getText().length().isEmpty())
    {
        // Somehow reject the key press?
    }
}

上述方法不适用于复制粘贴...或大量其他内容等。像这样使用按键侦听器似乎是个糟糕的主意。必须有更好的东西吗?有没有属性的文本框只允许输入一定的字符,或者只允许输入一定数量的字符?

谢谢!

您可以使用 TextFormatter 来做到这一点。 TextFormatter 可以修改对文本字段中的文本所做的更改,如果它具有与之关联的过滤器。过滤器是一个函数,它接受一个 TextFormatter.Change 对象和 returns 一个相同类型的对象。它可以 return null 完全否决或修改它。

所以你可以做到

TextField textField = new TextField();
textField.setTextFormatter(new TextFormatter<String>((Change change) -> {
    String newText = change.getControlNewText();
    if (newText.length() > 1) {
        return null ;
    } else {
        return change ;
    }
});

请注意,TextFormatter 也可用于将文本转换为您喜欢的任何类型的值。在您的情况下,将文本转换为 Integer 并且只允许整数输入是有意义的。作为对用户体验的最后补充,您可以修改更改,以便在用户键入数字时替换当前内容(而不是在字符太多时忽略它)。整个事情看起来像这样:

    TextField textField = new TextField();

    // converter that converts text to Integers, and vice-versa:
    StringConverter<Integer> stringConverter = new StringConverter<Integer>() {

        @Override
        public String toString(Integer object) {
            if (object == null || object.intValue() == 0) {
                return "";
            }
            return object.toString() ;
        }

        @Override
        public Integer fromString(String string) {
            if (string == null || string.isEmpty()) {
                return 0 ;
            }
            return Integer.parseInt(string);
        }
        
    };
    
    // filter only allows digits, and ensures only one digit the text field:
    UnaryOperator<Change> textFilter = c -> {
    
        // if text is a single digit, replace current text with it:            
        if (c.getText().matches("[1-9]")) {
            c.setRange(0, textField.getText().length());
            return c ;
        } else 
        // if not adding any text (delete or selection change), accept as is    
        if (c.getText().isEmpty()) {
            return c ;
        }
        // otherwise veto change
        return null ;
    };
    
    TextFormatter<Integer> formatter = new TextFormatter<Integer>(stringConverter, 0, textFilter);
    
    formatter.valueProperty().addListener((obs, oldValue, newValue) -> {
        // whatever you need to do here when the actual value changes:
        int old = oldValue.intValue();
        int updated = newValue.intValue();
        System.out.println("Value changed from " + old + " to " + updated);
    });
    
    textField.setTextFormatter(formatter);