android edittext 限制輸入框小數位元

來源:互聯網
上載者:User

標籤: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 限制輸入框小數位元

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.