コード例 #1
0
  /**
   * Sets precision of this selector. Precision defines how many digits after decimal point can be
   * entered. By default this is set to 0, meaning that only integers are allowed. Setting precision
   * to 1 would allow 0.0, precision = 2 would allow 0.00 and etc.
   */
  public void setPrecision(final int precision) {
    if (precision < 0) throw new IllegalStateException("Precision can't be < 0");
    this.precision = precision;

    valueText.getValidators().clear();
    valueText.addValidator(boundsValidator); // Both need the bounds check
    if (precision == 0) {
      valueText.addValidator(Validators.INTEGERS);
      valueText.setTextFieldFilter(new DigitsOnlyFilter());
    } else {
      valueText.addValidator(Validators.FLOATS);
      valueText.addValidator(
          new InputValidator() {
            @Override
            public boolean validateInput(String input) {
              int dotIndex = input.indexOf('.');
              if (dotIndex == -1) return true;
              return input.length() - input.indexOf('.') - 1 <= precision;
            }
          });
      valueText.setTextFieldFilter(new FloatDigitsOnlyFilter(true));
    }
  }