为了账号安全,请及时绑定邮箱和手机立即绑定

限制Android EditText中的小数位数

限制Android EditText中的小数位数

慕村9548890 2019-10-06 15:47:08
我正在尝试编写一个可帮助您管理财务的应用程序。我正在使用一个EditText字段,用户可以在其中指定金额。我将设置inputType为numberDecimal可以正常使用,除了允许人们输入数字(例如,123.122这对金钱而言并不理想)之外,它设置得很好。有没有办法将小数点后的字符数限制为两个?
查看完整描述

3 回答

?
精慕HU

TA贡献1845条经验 获得超8个赞

不使用正则表达式的简单解决方案:


import android.text.InputFilter;

import android.text.Spanned;


/**

 * Input filter that limits the number of decimal digits that are allowed to be

 * entered.

 */

public class DecimalDigitsInputFilter implements InputFilter {


  private final int decimalDigits;


  /**

   * Constructor.

   * 

   * @param decimalDigits maximum decimal digits

   */

  public DecimalDigitsInputFilter(int decimalDigits) {

    this.decimalDigits = decimalDigits;

  }


  @Override

  public CharSequence filter(CharSequence source,

      int start,

      int end,

      Spanned dest,

      int dstart,

      int dend) {



    int dotPos = -1;

    int len = dest.length();

    for (int i = 0; i < len; i++) {

      char c = dest.charAt(i);

      if (c == '.' || c == ',') {

        dotPos = i;

        break;

      }

    }

    if (dotPos >= 0) {


      // protects against many dots

      if (source.equals(".") || source.equals(","))

      {

          return "";

      }

      // if the text is entered before the dot

      if (dend <= dotPos) {

        return null;

      }

      if (len - dotPos > decimalDigits) {

        return "";

      }

    }


    return null;

  }


}

使用方法:


editText.setFilters(new InputFilter[] {new DecimalDigitsInputFilter(2)});


查看完整回答
反对 回复 2019-10-06
?
一只萌萌小番薯

TA贡献1795条经验 获得超7个赞

此实现InputFilter解决了问题。


import android.text.SpannableStringBuilder;

import android.text.Spanned;

import android.text.method.DigitsKeyListener;


public class MoneyValueFilter extends DigitsKeyListener {

    public MoneyValueFilter() {

        super(false, true);

    }


    private int digits = 2;


    public void setDigits(int d) {

        digits = d;

    }


    @Override

    public CharSequence filter(CharSequence source, int start, int end,

            Spanned dest, int dstart, int dend) {

        CharSequence out = super.filter(source, start, end, dest, dstart, dend);


        // if changed, replace the source

        if (out != null) {

            source = out;

            start = 0;

            end = out.length();

        }


        int len = end - start;


        // if deleting, source is empty

        // and deleting can't break anything

        if (len == 0) {

            return source;

        }


        int dlen = dest.length();


        // Find the position of the decimal .

        for (int i = 0; i < dstart; i++) {

            if (dest.charAt(i) == '.') {

                // being here means, that a number has

                // been inserted after the dot

                // check if the amount of digits is right

                return (dlen-(i+1) + len > digits) ? 

                    "" :

                    new SpannableStringBuilder(source, start, end);

            }

        }


        for (int i = start; i < end; ++i) {

            if (source.charAt(i) == '.') {

                // being here means, dot has been inserted

                // check if the amount of digits is right

                if ((dlen-dend) + (end-(i + 1)) > digits)

                    return "";

                else

                    break;  // return new SpannableStringBuilder(source, start, end);

            }

        }


        // if the dot is after the inserted part,

        // nothing can break

        return new SpannableStringBuilder(source, start, end);

    }

}


查看完整回答
反对 回复 2019-10-06
  • 3 回答
  • 0 关注
  • 883 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信