Recent projects need to enter a price, but do not want the user to enter too much after the decimal point, so I encapsulated one. When the user enters a decimal point to listen to the number of digits after the decimal point, as long as the greater than two bits immediately deleted, packaged well, can be used directly!
public static void Setpricepoint (final EditText EditText) {Edittext.addtextchangedlistener (new Textwatcher () {@ overridepublic void OnTextChanged (charsequence s, int start, int before,int count) {if (S.tostring (). Contains (".")) {if (s.length ()-1-s.tostring (). IndexOf (".") > 2) {s = s.tostring (). Subsequence (0,s.tostring (). IndexOf (".") + 3); EDI Ttext.settext (s); Edittext.setselection (S.length ());}} if (s.tostring (). Trim (). substring (0). Equals (".")) {s = "0" + s;edittext.settext (s); edittext.setselection (2);} if (s.tostring (). StartsWith ("0") && s.tostring (). Trim (). Length () > 1) {if (!s.tostring (). substring (1, 2). Equals (".")) {Edittext.settext (s.subsequence (0, 1)); edittext.setselection (1); return;}}} @Overridepublic void Beforetextchanged (charsequence s, int start, int count,int after) {} @Overridepublic void Aftertextch Anged (Editable s) {//TODO auto-generated Method stub}});
Android edittext Listening decimal point allows the user to enter only two digits after the decimal point