2

in my application i have a Custom text box with BasicEditField.FILTER_NUMERIC. When the user enter the value in the field the comma should be added to the Currency format .

EX:1,234,567,8.... like this.

In my code i tried like this.

protected boolean keyUp(int keycode, int time) {
    String entireText = getText();
    if (!entireText.equals(new String(""))) {
        double val = Double.parseDouble(entireText);

        String txt = Utile.formatNumber(val, 3, ",");// this will give the //comma separation format 
        setText(txt);// set the value in the text box
    }
    return super.keyUp(keycode, time);
}

it will give the correct number format... when i set the value in the text box it will through the IllegalArgumentException. I know BasicEditField.FILTER_NUMERIC will not allow the charector like comma(,)..

How can i achieve this?

Nate
  • 31,017
  • 13
  • 83
  • 207
prakash
  • 644
  • 4
  • 21
  • 2
    You need to make your own filter extending TextFielter, http://www.blackberry.com/developers/docs/6.0.0api/net/rim/device/api/ui/text/TextFilter.html. And later need to set that filter to the BasicEditField instance. – Rupak Jul 30 '12 at 10:23
  • 2
    BasicEditField.FILTER_NUMERIC is connected to locale of your BlackBerry. Some locales using different approach for periods separation. – Eugen Martynov Jul 30 '12 at 10:34

1 Answers1

2

I tried this way and it works fine...

public class MyTextfilter extends TextFilter {
private static TextFilter _tf = TextFilter.get(TextFilter.REAL_NUMERIC);

public char convert(char character, int status) {
    char c = 0;

    c = _tf.convert(character, status);
    if (c != 0) {
        return c;
    }

    return 0;
}

public boolean validate(char character) {
    if (character == Characters.COMMA) {
        return true;
    }

    boolean b = _tf.validate(character);
    if (b) {
        return true;

    }

    return false;
}
}

and call like this

editField.setFilter(new MyTextfilter());
prakash
  • 644
  • 4
  • 21