Amount Input Box

Source: Internet
Author: User

import android.text.inputfilter;import android.text.spanned;import java.util.regex.matcher; import java.util.regex.pattern;/*** joy on 2015/12/17.**  Amount Input Format */public class  EditInputFilter implements InputFilter {/***  Maximum number, we take int type maximum value */public static  final int MAX_VALUE = 2147483647;/***  number of digits after the decimal point */public static final  int PONTINT_LENGTH = 2; Pattern p;public editinputfilter () {p = pattern.compile ("[0-9]*");    // }/***  source     the new input string except for the number *  start     The new input string starts subscript, typically 0*  end     the new input string end subscript, usually the source length -1*  dest      before entering the contents of the text box *  dstart     original content starting coordinates, generally 0*  dend      original content endpoint coordinates, typically dest length -1*/@Overridepublic  charsequence filter (charsequence src,&nBsp;int start, int end,spanned dest, int dstart, int dend)  { String oldtext =  dest.tostring (); System.out.println (OldText);//Verify the deletion of keys such as if  ("". Equals (Src.tostring ()))  {return null;} Verify non-numeric or decimal point matcher m = p.matcher (SRC); if (Oldtext.contains (".")) {//If there is already a decimal point, you can only enter the number if (!m.matches ()) {return null;}} else{//You can enter a decimal point and a number if the decimal point is not entered (!m.matches ()  && !src.equals (".")  ) {return null;}} Verify the size of the input amount if (!src.tostring (). Equals ("")) {double dold = double.parsedouble (oldtext+src.tostring ()); if (Dold > max_value) {return dest.subsequence (dstart, dend);} Else if (Dold == max_value) {if (src.tostring (). Equals (".")) {return dest.subsequence (dstart, dend);}}} Verify that the decimal digit precision is correct if (Oldtext.contains (".")) {Int index = oldtext.indexof ("."); int len = dend – index;//decimal place can only be 2-bit if (len > pontint_leNgth) {charsequence newtext = dest.subsequence (dstart, dend); return newText;}} Return dest.subsequence (Dstart, dend)  +src.tostring ();}}


Inputfilter[] Filters = {new Editinputfilter ()};

Edit_takeout_money.setfilters (filters);


Amount 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.