Android EditText limit the number of decimal digits in the input box

Source: Internet
Author: User

Look at the XML layout file first

<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=" "            android:singleline=" true "            Android:textcolor= "@color/txt_black1"            android:textcolorhint= "@color/txt_gray2"            android:textsize= "@ Dimen/textsize16 "/>

Android:inputtype= "Numberdecimal"
The attribute of this sentence is that you can enter decimals

/** the number of decimal places in the input box */private static final int decimal_digits = 1;                

/** *  Set the number of decimal digits control */    inputfilter lengthfilter = new Inputfilter () {Public           charsequence filter ( Charsequence source, int start, int end,                   spanned dest, int dstart, int dend) {               //delete special characters, return directly               if ("". Equals (S Ource.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});  
This is OK, very simple, actually this inputfilter is very powerful drop

Android EditText limit the number of decimal digits in the input box

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.