2012-07-30 92 views
2

在我的应用程序中我有一个自定义文本框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 ..

我该如何做到这一点?

+2

你需要让自己的滤波扩展TextFielter,HTTP://www.blackberry。 COM /开发者/文档/ 6.0.0api /净/轮辋/设备/ API/UI /文本/ TextFilter.html。之后需要将该过滤器设置为BasicEditField实例。 – Rupak 2012-07-30 10:23:06

+2

BasicEditField.FILTER_NUMERIC已连接到BlackBerry的语言环境。一些区域设置使用不同的方法进行时段分隔。 – 2012-07-30 10:34:20

回答

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

不错。将您的答案标记为已解决。它会帮助别人。 – Rupak 2012-07-30 17:24:01

+0

这也适用于我 – Lucas 2012-11-18 15:43:28