Android development, enter the number of digits before the decimal point in the control amount field in editText

editText.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL);
		//The default control input is 10 digits, 7 digits before the decimal point, 2 digits after the decimal point, one decimal place, a total of 10 digits
		InputFilter[] filters={new CashierInputFilter(10),new InputFilter.LengthFilter(10)};
		
		editText.setFilters(filters);




----------------------------------------------

/**
 * @author liudong</br>
 * Description: Filter input specified amount format in EditText input box</br>
 * 1. 7 digits before the decimal point</br>
 * 2. 2 digits after the decimal point
 *
 * */
public class CashierInputFilter implements InputFilter {
	
	 private Pattern mPattern;
	 // maximum amount entered
	 private  final int MAX_VALUE = Integer.MAX_VALUE;
	 
	 //2 digits after the decimal point
	 private  final int POINTER_AFTER_LENGTH = 2;
	 
	 //7 digits before the decimal point
     private   int POINTER_BEFORE_LENGTH = 7;

	 private  final String POINTER = ".";

	 private static final String ZERO = "0";

	 public CashierInputFilter() {
	        mPattern = Pattern.compile("([0-9]|\\.)*");
	 }
	 
	 public CashierInputFilter(int _maxLength) {
	        mPattern = Pattern.compile("([0-9]|\\.)*");
	        POINTER_BEFORE_LENGTH=_maxLength-POINTER_AFTER_LENGTH-1;
	        
	 }


	 /**
	     * @param source new input string
	     * @param start The starting subscript of the newly input string, usually 0
	     * @param end The subscript of the new input string end point, usually source length-1
	     * @param dest Enter the text box content before
	     * @param dstart The starting coordinate of the original content, usually 0
	     * @param dend The coordinates of the end point of the original content, generally dest length-1
	     * @return input content
	     */

	@Override
	public CharSequence filter(CharSequence source, int start, int end,
			Spanned dest, int dstart, int dend) {
		
		String sourceText = source.toString();
        String destText = dest.toString ();

        // Verify delete and other buttons
        if (TextUtils.isEmpty(sourceText)) {
                return "";
        }

        Matches matches = mPattern.matches (source);
        //If the decimal point has been entered, only numbers can be entered
        if(destText.contains(POINTER)) {
            if (!matcher.matches()) {
                    return "";
            } else {
                if (POINTER.equals(source)) { //Only one decimal point can be entered
                    return "";
                }
            }

           
            int index = destText.indexOf(POINTER);
            //Verify the precision of the decimal point to ensure that only two digits can be entered after the decimal point
            int afterLength = dend - index;
            
           // After input, the modification cannot be controlled, // Verify the precision of the decimal point to ensure the number of digits before the decimal point
            
           if(dstart==POINTER_BEFORE_LENGTH && index==POINTER_BEFORE_LENGTH)
            {
               return dest.subSequence(dstart, dend);
            	
            }
            
            
            //Verify the precision of the decimal point to ensure that only two digits can be entered after the decimal point
            if (afterLength > POINTER_AFTER_LENGTH) {
                return dest.subSequence(dstart, dend);
            }
           
            
        } else {
            //If no decimal point is entered, only the decimal point and numbers can be entered, but the decimal point and 0 cannot be entered in the first place
            if (!matcher.matches()) {
                return "";
            } else {
            	 //Verify the precision of the decimal point to ensure that only 7 digits can be entered before the decimal point
                
                if(dstart==POINTER_BEFORE_LENGTH){
                	if(sourceText.contains(POINTER))
                	  return dest.subSequence(dstart,dend)+ sourceText+ZERO+ZERO;
                	else
                	  return dest.subSequence(dstart,dend)+POINTER+ZERO+ZERO;
                }
                if ((POINTER.equals(source) || ZERO.equals(source)) && TextUtils.isEmpty(destText)) {
                    return ZERO+POINTER+ZERO+ZERO;
                }
            }
        }

        // Verify the size of the input amount
        double sumText = Double.parseDouble(destText + sourceText);
        if (sumText >=MAX_VALUE) {
            return dest.subSequence(dstart, dend);
        }

        return dest.subSequence(dstart, dend) + sourceText;
    }


}

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326689783&siteId=291194637