【问题标题】:Comma separation in the Text Field in Blackberry黑莓中文本字段中的逗号分隔
【发布时间】:2012-07-31 10:00:20
【问题描述】:

在我的应用程序中,我有一个带有 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 不会允许像逗号(,)这样的字符..

我怎样才能做到这一点?

【问题讨论】:

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


【解决方案1】:

我试过这个方法,效果很好……

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-16
    • 2016-04-24
    • 2011-11-14
    相关资源
    最近更新 更多