在我的应用程序中,我有一个带有 BasicEditField.FILTER_NUMERIC
的自定义文本框。当用户在字段中输入值时,应将逗号添加到货币格式中。
例如: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
不允许像逗号(,)这样的字符..
我怎样才能实现这个目标?
最佳答案
我尝试了这个方法,效果很好...
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());
关于blackberry - Blackberry 文本字段中的逗号分隔,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11718730/