【问题标题】:Edittext enter percentage value with lessthen 100 and after point only two digit acceptableEdittext 输入小于 100 的百分比值,点后仅可接受两位数
【发布时间】:2019-02-26 07:24:58
【问题描述】:

我有EditText,我用它来输入百分比值。我限制用户不能输入超过 100 的值,它工作正常。我也可以输入百分比的小数部分,但只允许小数点后两位数 (.) 即 99.95

我希望它以编程方式实现,因为我在每次用于输入值并在其他字段中更改的弹出窗口中使用此 EditText。我尝试了以下代码来实现我的目标。

if (title.contains("Deposit")) {
    edt_get_value_popup_value.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL | InputType.TYPE_NUMBER_FLAG_SIGNED);
    edt_get_value_popup_value.setFilters(new InputFilter[]{new InputFilterMinMax("1", "100")});
}

我正在使用过滤器类转换为百分比,请参见以下代码:

public class InputFilterMinMax implements InputFilter {

   private Float min, max;

   public InputFilterMinMax(Float min, Float max) {
        this.min = min;
        this.max = max;
   }

   public InputFilterMinMax(String min, String max) {
        this.min = Float.parseFloat(min);
        this.max = Float.parseFloat(max);
    }

   @Override
   public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
       try {
            Float input = Float.parseFloat(dest.toString() + source.toString());
            if (isInRange(min, max, input)) return null;
        } catch (NumberFormatException nfe) {
        }
        return "";
   }

   private boolean isInRange(Float a, Float b, Float c) {
        return b > a ? c >= a && c <= b : c >= b && c <= a;
   }
}

如何设置EditText中小数部分的限制?

【问题讨论】:

标签: java android android-edittext input-filter


【解决方案1】:

您可以在应用一些更改后使用如下输入过滤器:

 public class InputFilterMinMax implements InputFilter {

    private Float min, max;

    public InputFilterMinMax(Float min, Float max) {
        this.min = min;
        this.max = max;
    }

    public InputFilterMinMax(String min, String max) {
        this.min = Float.parseFloat(min);
        this.max = Float.parseFloat(max);
    }

    @Override
    public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
        try {
            Float input = Float.parseFloat(dest.toString() + source.toString());
            String inputValue = (dest.toString() + source.toString());
            if (isInRange(min, max, input, inputValue)) return null;
        } catch (NumberFormatException nfe) {
        }
        return "";
    }

    private boolean isInRange(Float min, Float max, Float input, String inputValue) {
        if (inputValue.contains(".") && (inputValue.split("\\.").length > 1)) {
            return (max > min ? input >= min && input <= max : input >= max && input <= min) && (inputValue.split("\\.")[1].length() < 3);
        } else {
            return (max > min ? input >= min && input <= max : input >= max && input <= min);
        }
    }
}

【讨论】:

  • @Mayur Karmur 明白了吗?
  • @Mayur Karmur 仍然面临问题或卡在任何地方?
【解决方案2】:

使用edittext的maxLength属性,

<EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Customization"
        android:maxLength="4"
        android:singleLine="true"
        />

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-26
    • 1970-01-01
    • 2013-06-14
    • 2015-11-26
    相关资源
    最近更新 更多