2012-05-24 42 views

回答

1

一個更好的解決辦法是通過添加適當的TextFilter來控制用戶輸入您的編輯字段。這還具有修改屏幕鍵盤的額外好處,以便與裝備如此的設備上的過濾器相匹配。

這裏是結合兩個內置的文本過濾器,使一個只允許上字母和數字的操作爲例:

import net.rim.device.api.ui.text.TextFilter; 
import net.rim.device.api.system.Characters; 

/** 
* A TextFilter class to filter for station identifiers 
*/ 
private static class StationFilter extends TextFilter { 

// Order of the supporting filters is important, NUMERIC will convert 
// letters to numbers if it gets them first. 
private static TextFilter[]  _tf = { 
    TextFilter.get(TextFilter.NUMERIC), 
    TextFilter.get(TextFilter.UPPERCASE) 
}; 

// Convert using the first supporting filter that has a conversion 
public char convert(char character, int status) { 
    char c = 0; 

    for (int i = _tf.length - 1; i >= 0; i--) { 
    c = _tf[i].convert(character, status); 
    if (c != 0) { 
    return c; 
    } 
    } 

    return 0; 
} 

// Validate a space for separator, then by supporting filter 
public boolean validate(char character) { 
    if (character == Characters.SPACE) { 
    return true; 
    } 

    for (int i = _tf.length - 1; i >= 0; i--) { 
    boolean b = _tf[i].validate(character); 
    if (b) { 
    return true; 
    } 
    } 

    return false; 
} 
} 
+0

,但什麼用戶鍵盤錄入的情況? – jibysthomas

+0

感謝它的工作對於觸摸和鍵盤!!! ... – jibysthomas

+0

是的,它也適用於物理鍵盤,只是不能改變鍵盤來匹配過濾器。 – Richard