android 限制EditText输入数字的范围大小

一,要想限制EditText的输入大小,经过学习和测试;最好是创建自己的EditText(当然是继承的

      

public class DecimalEditText extends android.support.v7.widget.AppCompatEditText {


    /**
     * 保留小数点前多少位,默认三位,既到千位
     */
    private int mDecimalStarNumber = 3;

    /**
     * 保留小数点后多少位,默认两位
     */
    private int mDecimalEndNumber = 2 ;

    public DecimalEditText(Context context) {
        this(context, null);
    }

    public DecimalEditText(Context context, AttributeSet attrs) {
        this(context, attrs, R.attr.editTextStyle);
    }

    public DecimalEditText(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.DecimalEditText);
        
        mDecimalStarNumber = typedArray.getInt(R.styleable.DecimalEditText_decimalStarNumber, mDecimalStarNumber);
        mDecimalEndNumber = typedArray.getInt(R.styleable.DecimalEditText_decimalEndNumber, mDecimalEndNumber);
        typedArray.recycle();
        init();
    }

    /**
     * 初始化
     */
    private void init() {
        setFilters(new InputFilter[]{new InputFilter() {
            @Override
            public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
                String lastInputContent = dest.toString();

                LogUtils.d("source->" + source + "--start->" + start + " " +
                        "--lastInputContent->" + lastInputContent + "--dstart->" + dstart + "--dend->" + dend);


                if (source.equals(".") && lastInputContent.length() == 0) {
                    return "0.";
                }

                if (!source.equals(".") && !source.equals("") && lastInputContent.equals("0")) {
                    return ".";
                }

                if (source.equals(".") && lastInputContent.contains(".")) {
                    return "";
                }

                if (lastInputContent.contains(".")) {
                    int index = lastInputContent.indexOf(".");
                    if (dend - index >= mDecimalEndNumber + 1) {
                        return "";
                    }
                } else {
                    if (!source.equals(".") && lastInputContent.length() >= mDecimalStarNumber) {
                        return "";
                    }
                }

                return null;
            }
        }});
    }


    public int getDecimalStarNumber() {
        return mDecimalStarNumber;
    }

    public void setDecimalStarNumber(int decimalStarNumber) {
        mDecimalStarNumber = decimalStarNumber;
    }

    public int getDecimalEndNumber() {
        return mDecimalEndNumber;
    }

    public void setDecimalEndNumber(int decimalEndNumber) {
        mDecimalEndNumber = decimalEndNumber;
    }
}
二,对应的属性

    <declare-styleable name="DecimalEditText">
        <attr name="decimalStarNumber" format="integer"/>
        <attr name="decimalEndNumber" format="integer"/>
    </declare-styleable>
三,对应要实现的功能,Activity就非常简单了

    

class Main17Activity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main17)
    }
}

四,对应布局为

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.jgapplication.view.DecimalEditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="numberDecimal"
        app:decimalEndNumber="2"
        app:decimalStarNumber="4"/>

</LinearLayout>


  

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值