黑莓文本字段中的逗号分隔

时间:2012-07-30 09:31:21

标签: blackberry blackberry-eclipse-plugin blackberry-jde

在我的应用程序中,我有一个带有BasicEditField.FILTER_NUMERIC的自定义文本框。当用户在字段中输入值时,应将逗号添加到货币格式。

EX:1,234,567,8 ....就像这样。

在我的代码中,我试过这样。

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

它将给出正确的数字格式...当我在文本框中设置值时,它将通过IllegalArgumentException。我知道BasicEditField.FILTER_NUMERIC不会允许charector像逗号(,)..

我怎样才能实现这个目标?

1 个答案:

答案 0 :(得分:2)

我试过这种方式,它运作正常......

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

并像这样打电话

editField.setFilter(new MyTextfilter());