Пример #1
0
  public void toggleSign() {
    DecimalFormatSymbols symbols = ((DecimalFormat) formatter).getDecimalFormatSymbols();
    char minus = symbols.getMinusSign();
    String exp = symbols.getExponentSeparator();
    String text = getText().toString();

    int expPos = text.lastIndexOf(exp);
    if (expPos == -1) {
      if (text.charAt(0) == minus) {
        text = text.substring(1);
      } else {
        text = minus + text;
      }

    } else { // Exponent entered
      try {
        if (text.charAt(expPos + exp.length()) == minus) {
          text = text.substring(0, expPos) + exp + text.substring(expPos + exp.length() + 1);
        } else {
          text = text.substring(0, expPos) + exp + minus + text.substring(expPos + exp.length());
        }
      } catch (StringIndexOutOfBoundsException e) {
        text = text + minus;
      }
    }

    setText(text);
  }
Пример #2
0
 public static VerifyListener getNumberVerifyListener(Locale locale) {
   DecimalFormatSymbols symbols = DecimalFormatSymbols.getInstance(locale);
   final char[] allowedChars =
       new char[] {
         symbols.getDecimalSeparator(),
         symbols.getGroupingSeparator(),
         symbols.getMinusSign(),
         symbols.getZeroDigit(),
         symbols.getMonetaryDecimalSeparator(),
         '+'
       };
   final String exponentSeparator = symbols.getExponentSeparator();
   return new VerifyListener() {
     @Override
     public void verifyText(VerifyEvent e) {
       for (int i = 0; i < e.text.length(); i++) {
         char ch = e.text.charAt(i);
         if (!Character.isDigit(ch)
             && !ArrayUtils.contains(allowedChars, ch)
             && exponentSeparator.indexOf(ch) == -1) {
           e.doit = false;
           return;
         }
       }
       e.doit = true;
     }
   };
 }