|
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 the 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]*"); In addition to the number of other
}
/**
* Source New Input string
* Start new input string start subscript, typically 0
* End of the new input string endpoint subscript, typically source length-1
* dest input before text box content
* DStart original content starting coordinates, generally 0
* DEnd Original content endpoint coordinates, generally dest length-1
*/
@Override
Public charsequence filter (charsequence src, int start, int end,
spanned dest, int dstart, int dend) {
String OldText = dest.tostring ();
System.out.println (OldText);
Verify deletion and other keys
if ("". Equals (Src.tostring ())) {
return null;
}
Verifying a non-digit or decimal point
Matcher m = p.matcher (SRC);
if (Oldtext.contains (".")) {
If you already have a decimal point, you can only enter a number
if (!m.matches ()) {
return null;
}
}else{
If you do not enter a decimal point, you can enter a decimal point and number
if (!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 position accuracy is correct
if (Oldtext.contains (".")) {
int index = Oldtext.indexof (".");
int len = Dend–index;
Decimal digits can only be 2 digits
if (Len > Pontint_length) {
Charsequence NewText = dest.subsequence (DStart, dend);
return newtext;
}
}
Return Dest.subsequence (DStart, DEnd) +src.tostring ();
}
}
How to use our custom class, simply define the EditText control Edit_takeout_money in the corresponding activity, and then:
Inputfilter[] Filters = {new Editinputfilter ()};
Edit_takeout_money.setfilters (filters);
It's OK.
|