如何让libgdx文本框忽略特定字符?

3

大家好,我遇到了一个问题,关于libgdx的TextField,我似乎无法解决。我想忽略某些字符,例如使用“Shift”输入大写字符时,不会在文本字段中键入空格。截至目前,我的代码如下:

accField.setTextFieldListener(new TextFieldListener() {

  public void keyTyped(TextField textField, char c)  {

        if (c == 'a') {
        //Something here that does the replacement maybe?                                


        }


}

});

你见过Textfield.TextfieldFilter吗?这是你在找的东西吗? - undefined
看起来很有趣...我该如何以良好的方式实现它呢? - undefined
我写了一个答案。希望能对你有所帮助。但是我从未尝试过它。如果有错误,请告诉我并纠正我,因为我也想在我的游戏中使用这些过滤器:P - undefined
2个回答

9

我以前没有使用过TextFieldFilters,因为我对libgdx几乎是新手。但是据我所知,您可以像这样使用它们:

myTextfield.setTextFieldFilter(new TextFieldFilter() {

    // Accepts all Characters except 'a' 
    public  boolean acceptChar(TextField textField, char c) {
         if (c == 'a')
               return false;
         return true;
    }
});

希望能对您有所帮助。如果它有效,请告诉我,因为我想使用这个TextFieldFilters :P


酷!现在我知道我可以这样使用它们了,谢谢! - undefined
正则表达式也可以工作,Character.toString(c).matches("^[a-zA-Z0-9]") 只匹配字母和数字。 - undefined

0

你只能用它来输入字母和数字。

myTextfield.setTextFieldFilter(new TextField.TextFieldFilter() {
            // Accepts all Alphanumeric Characters except
            public boolean acceptChar(TextField textField, char c) {
                if (Character.toString(c).matches("^[a-zA-Z]")) {
                    return true;
                }
                return false;
            }
        });

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接