EditText limits the number of digits in the input integer and decimal places. edittext integer
For example, this article mainly describes how to limit the number of digits in an integer and decimal place that can be entered in EditText.
Recently, because the company's business needs include the price input function, the requirement is that the number of digits must be an integer and the number of decimal digits can be entered... Well, developers have to be busy for a long time in a single product sentence. During normal work, the length of the input content is generally restricted. Such requirements that limit the integer and decimal places of numeric input are not very common. At that time, when I did this, I also felt that it was too troublesome. Later I thought, as long as the analysis was clear, it was quite easy. We hope to help people who need it.
The usage is as follows:
EditText etTest = (EditText) findViewById (R. id. et_test); etTest. setInputType (InputType. TYPE_CLASS_NUMBER | InputType. TYPE_NUMBER_FLAG_DECIMAL); etTest. addTextChangedListener (new DecimalInputTextWatcher (etTest, 3, 5); // an integer can contain up to three digits, and a decimal number can contain up to five digits.
The implementation principle is to implement implements TextWatcher and override the method:
public void afterTextChanged(Editable editable){}
DecimalInputTextWatcher source code:
Import android. text. editable; import android. text. inputFilter; import android. text. textWatcher; import android. widget. editText;/*** EditText limits the number of digits in the input integer and decimal places * the entire digit is unlimited by default, and the maximum number of decimal places is 2 * <p> * Created by dingzuoqiang on decimal places /11/24. * 530858106@qq.com */public class DecimalInputTextWatcher implements TextWatcher {/*** EditText */private EditText editText = null for the DecimalInputTextWatcher needs to be set;/*** default decimal bit Number of 2 digits */private static final int DEFAULT_DECIMAL_DIGITS = 2; private int decimalDigits; // number of decimal digits private int integerDigits; // Number of integer digits public DecimalInputTextWatcher (EditText editText) {this. editText = editText; this. decimalDigits = DEFAULT_DECIMAL_DIGITS;}/*** @ param editText * @ param decimalDigits decimal digits */public DecimalInputTextWatcher (EditText editText, int decimalDigits) {this. edit Text = editText; if (decimalDigits <= 0) throw new RuntimeException ("decimalDigits must> 0"); this. decimalDigits = decimalDigits;}/*** @ param editText * @ param integerDigits integer digits * @ param decimalDigits decimal digits */public comment (EditText editText, int integerDigits, int decimalDigits) {this. editText = editText; if (integerDigits <= 0) throw new RuntimeException ("integer Digits must> 0 "); if (decimalDigits <= 0) throw new RuntimeException (" decimalDigits must> 0 "); this. integerDigits = integerDigits; this. decimalDigits = decimalDigits;} @ Override public void beforeTextChanged (CharSequence charSequence, int I, int i1, int i2) {}@ Override public void onTextChanged (CharSequence charSequence, int I, int i1, int i2) {}@ Override public void afterTextChanged (Editab Le editable) {String s = editable. toString (); editText. removeTextChangedListener (this); if (s. contains (". ") {if (integerDigits> 0) {editText. setFilters (new InputFilter [] {new InputFilter. lengthFilter (integerDigits + decimalDigits + 1)});} if (s. length ()-1-s. indexOf (". ")> decimalDigits) {s = s. substring (0, s. indexOf (". ") + decimalDigits + 1); editable. replace (0, editable. length (), s. trim () ;}} Else {if (integerDigits> 0) {editText. setFilters (new InputFilter [] {new InputFilter. lengthFilter (integerDigits + 1)}); if (s. length ()> integerDigits) {s = s. substring (0, integerDigits); editable. replace (0, editable. length (), s. trim () ;}}} if (s. trim (). equals (". ") {s =" 0 "+ s; editable. replace (0, editable. length (), s. trim ();} if (s. startsWith ("0") & s. trim (). length ()> 1) {if (! S. substring (1, 2 ). equals (". ") {editable. replace (0, editable. length (), "0") ;}} editText. addTextChangedListener (this );}}
Source code download