2014-01-30 61 views
2

嗨我困住了一個問題,我似乎無法解決關於TextField for libgdx。我想忽略某些字符,例如在使用「shift」作爲大寫字符時,不要在文本字段中寫入空格。目前我已經寫過這樣的內容。製作libgdx文本字段會忽略某些字符?

accField.setTextFieldListener(new TextFieldListener() { 

    public void keyTyped(TextField textField, char c) { 

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


     } 


} 

}); 
+0

你見過Textfield.TextfieldFilter嗎?這是你想要的? – Springrbua

+0

這看起來很有趣..我怎麼才能以一種好的方式實現它? – Lucas

+0

我寫了一個答案。希望它可以幫助你。但我從來沒有嘗試過。請告訴我,如果這是錯誤的,糾正我,因爲我也想用我的遊戲過濾器:P – Springrbua

回答

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

+0

這工作得很好。謝謝:) – Lucas

+0

酷:P現在我知道我可以像這樣使用它們:P謝謝:) – Springrbua

+0

正則表達式的工作原理,'Character.toString(c).matches(「^ [a-zA-Z0-9] 「)'只有字母數字和數字。 – Madmenyo

-1

您只能將它用於字母數字輸入。

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