標籤:android edittext 小數位元
先看下XML布局檔案
<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" android:textColor="@color/txt_black1" android:textColorHint="@color/txt_gray2" android:textSize="@dimen/textsize16" />
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 }); 這樣就OK了,很簡單吧,其實這個InputFilter很強大滴
android edittext 限制輸入框小數位元