android edittext 限制输入框小数位数

来源:互联网 发布:自动抠图软件 编辑:程序博客网 时间:2024/06/05 10:55

原地址:http://blog.163.com/shexinyang@126/blog/static/136739312201522644825748/


<EditText            android:id="@+id/et"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:layout_toLeftOf="@+id/tv_count"            android:background="@null"            android:hint="@string/free_txt"            android:inputType="numberDecimal"            android:maxLength="10"            android:singleLine="true"<pre name="code" class="java">


android:inputType="numberDecimal"

这句的属性是可输入小数


/** 输入框小数的位数*/private static final int DECIMAL_DIGITS = 1; 


/** *  设置小数位数控制    */    InputFilter lengthfilter = new InputFilter() {           public CharSequence filter(CharSequence source, int start, int end,                   Spanned dest, int dstart, int dend) {               // 删除等特殊字符,直接返回               if ("".equals(source.toString())) {                   return null;               }               String dValue = dest.toString();               String[] splitArray = dValue.split("//.");               if (splitArray.length > 1) {                   String dotValue = splitArray[1];                   int diff = dotValue.length() + 1 - DECIMAL_DIGITS;                   if (diff > 0) {                       return source.subSequence(start, end - diff);                   }               }               return null;           }       };   


mEt.addTextChangedListener(mTextWatcher);mEt.setFilters(new InputFilter[] { lengthfilter });  




0 0
原创粉丝点击