Android implementation EditText Input amount _android

Source: Internet
Author: User

EditText is a very useful control in Android, and there are many inputtype that can be used to achieve different input effects, as shown in the following figure:

For example, password input, digital input and so on. But recently in the project to achieve edittext input amount, the amount of restrictive factors, such as, can only enter numbers and decimal point, the first can not enter 0 and decimal point, only two digits after the decimal point, and so on, these only with InputType is not possible, Today, it is realized by Inputfilter.

1. First you need to set the EditText InputType to the following format to ensure that users can only enter decimal points and numbers

Android:inputtype= "Numberdecimal" or Setinputtype (Inputtype.type_number_flag_decimal)

2. Custom Inputfilter, code as follows:

Cashierinputfilter.java

Package com.xylpay.utils; 
Import Android.text.InputFilter; 
Import android.text.Spanned; 
Import Android.text.TextUtils; 
Import Java.util.regex.Matcher; 
Import Java.util.regex.Pattern; 
 /** * Created by Jackie on 2016/1/30. 
  * Filter User input can only be for the amount format * * public class Cashierinputfilter implements Inputfilter {pattern Mpattern; 
  The maximum amount entered private static final int max_value = Integer.max_value; 
  Number of digits after the decimal point private static final int pointer_length = 2; 
  Private static final String pointer = "."; 
  private static final String ZERO = "0"; Public Cashierinputfilter () {mpattern = Pattern.compile ([0-9]|\\.) 
  *"); 
   /** * @param source New Input String * @param start subscript of the new input string, typically 0 * @param end subscript for the new input string, typically the source length-1     * @param dest Input before the text box content * @param dstart original content starting coordinates, generally 0 * @param dend original content endpoint coordinates, generally dest length-1 * @return Input/@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 deletion, such as key if (Textutils.isempty (SourceText)) {return ""; 
    } Matcher Matcher = Mpattern.matcher (source); 
      If you have entered a decimal point, you can only enter a number if (Desttext.contains (pointer) {if (!matcher.matches ()) {return ""; 
        } else {if (pointer.equals (source)) {//can only enter a decimal points return ""; 
      }//Verify decimal point precision, ensure that only two-bit int index = DESTTEXT.INDEXOF (pointer) can be entered after the decimal point; 
      int length = Dend-index; 
      if (length > Pointer_length) {return dest.subsequence (DStart, dend); 
      If {//No decimal point is entered, you can enter only the decimal point and number, but first you cannot enter a decimal point and 0 if (!matcher.matches ()) {return ""; } else {if (pointer.equals (source) | | 
        Zero.equals (source) && Textutils.isempty (Desttext)) {return ""; }}//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; } 
}

Use the following methods:

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.