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