开发者

Set a BasicEditField Filter on BlackBerry

I want to make a BasicEditField that should accept only uppercase letters, lowercase letters and numeric values. It should not accept any other symbol. Is there any style bit ava开发者_如何学运维ilable for BlackBerry BasicEditField that makes it work this way? If not, is there anything else I can do?


I use these methods: TextFilter#get and BasicEditField#setFilter

mEdit = new EditField();
mEdit.setFilter(TextFilter.get(TextFilter.NUMERIC));

or

// Numeric filter that also allows minus sign. 
mEdit.setFilter(TextFilter.get(TextFilter.INTEGER));  

I think creating your own filter by overriding TextFilter can solve more difficult filtering task.


Perhaps it is not possible to combine them like that then, I didn't try it. Whenever I have used filters in my code I have always done it the second way I mentioned above. It is a bit more complicated but gives you a lot more control over what the field can and cannot accept. Check out the API documentation on the TextFilter class. As I said above you would just have to implement the convert and validate methods. I think in your case you wouldn't have to do anything in convert. But in the validate method you would just have to check that the character is in the range a-z A-Z or 0-9. If it is return true, otherwise return false. This will stop all unwanted characters from appearing in the field.


With a BasicEditField this is not easily implementable; however, the right solution would change the BasicEditField with AutoTextEditField. When creating the AutoTextEditField, use the constructor where you can pass in parameters. This should do it for you.

new AutoTextEditField("My label:", "initial value", 1024, 50,
                      TextFilter.LOWERCASE | TextFilter.NUMERIC);


mEdit.setFilter(TextFilter.get(TextFilter.NUMERIC)); works fine, also, I think, it doesnt take the minus sign, as while testing 'm not able to insert "-" minus sign in the field.


There is a constructor that allows you to specify the style of the field. You can then OR together the styles you want, like FILTER_UPPERCASE | FILTER_LOWERCASE | FILTER_NUMERIC.

Alternatively you can call setFilter and create your own subclass of TextFilter, which allows you to specify the exact behaviour you want by overwriting the convert and validate methods. Convert allows you to force certain things like automatically make all text entered uppercase etc. Validate allows you to block certain things from being entered, e.g. special characters. Check out the API for more information.


Kumar I also faced the same issue, used the below code to fix it -

The below script will allow Uppercase, Lowercase, Numeric values only.

setFilter(new TextFilter() {
        public boolean validate(char c) {
            return (Character.isDigit(c) || Character.isLowerCase(c) || Character.isUpperCase(c));
        }
        public char convert(char c, int status) {
            return c;
        }}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜