FileDocCategorySizeDatePackage
DigitsKeyListener.javaAPI DocAndroid 5.1 API6945Thu Mar 12 22:22:10 GMT 2015android.text.method

DigitsKeyListener

public class DigitsKeyListener extends NumberKeyListener
For digits-only text entry

As for all implementations of {@link KeyListener}, this class is only concerned with hardware keyboards. Software input methods have no obligation to trigger the methods in this class.

Fields Summary
private char[]
mAccepted
private boolean
mSign
private boolean
mDecimal
private static final int
SIGN
private static final int
DECIMAL
private static final char[]
CHARACTERS
The characters that are used.
private static DigitsKeyListener[]
sInstance
Constructors Summary
public DigitsKeyListener()
Allocates a DigitsKeyListener that accepts the digits 0 through 9.

        this(false, false);
    
public DigitsKeyListener(boolean sign, boolean decimal)
Allocates a DigitsKeyListener that accepts the digits 0 through 9, plus the minus sign (only at the beginning) and/or decimal point (only one per field) if specified.

        mSign = sign;
        mDecimal = decimal;

        int kind = (sign ? SIGN : 0) | (decimal ? DECIMAL : 0);
        mAccepted = CHARACTERS[kind];
    
Methods Summary
public java.lang.CharSequencefilter(java.lang.CharSequence source, int start, int end, android.text.Spanned dest, int dstart, int dend)

        CharSequence out = super.filter(source, start, end, dest, dstart, dend);

        if (mSign == false && mDecimal == false) {
            return out;
        }

        if (out != null) {
            source = out;
            start = 0;
            end = out.length();
        }

        int sign = -1;
        int decimal = -1;
        int dlen = dest.length();

        /*
         * Find out if the existing text has a sign or decimal point characters.
         */

        for (int i = 0; i < dstart; i++) {
            char c = dest.charAt(i);

            if (isSignChar(c)) {
                sign = i;
            } else if (isDecimalPointChar(c)) {
                decimal = i;
            }
        }
        for (int i = dend; i < dlen; i++) {
            char c = dest.charAt(i);

            if (isSignChar(c)) {
                return "";    // Nothing can be inserted in front of a sign character.
            } else if (isDecimalPointChar(c)) {
                decimal = i;
            }
        }

        /*
         * If it does, we must strip them out from the source.
         * In addition, a sign character must be the very first character,
         * and nothing can be inserted before an existing sign character.
         * Go in reverse order so the offsets are stable.
         */

        SpannableStringBuilder stripped = null;

        for (int i = end - 1; i >= start; i--) {
            char c = source.charAt(i);
            boolean strip = false;

            if (isSignChar(c)) {
                if (i != start || dstart != 0) {
                    strip = true;
                } else if (sign >= 0) {
                    strip = true;
                } else {
                    sign = i;
                }
            } else if (isDecimalPointChar(c)) {
                if (decimal >= 0) {
                    strip = true;
                } else {
                    decimal = i;
                }
            }

            if (strip) {
                if (end == start + 1) {
                    return "";  // Only one character, and it was stripped.
                }

                if (stripped == null) {
                    stripped = new SpannableStringBuilder(source, start, end);
                }

                stripped.delete(i - start, i + 1 - start);
            }
        }

        if (stripped != null) {
            return stripped;
        } else if (out != null) {
            return out;
        } else {
            return null;
        }
    
protected char[]getAcceptedChars()


    
       
        return mAccepted;
    
public intgetInputType()

        int contentType = InputType.TYPE_CLASS_NUMBER;
        if (mSign) {
            contentType |= InputType.TYPE_NUMBER_FLAG_SIGNED;
        }
        if (mDecimal) {
            contentType |= InputType.TYPE_NUMBER_FLAG_DECIMAL;
        }
        return contentType;
    
public static android.text.method.DigitsKeyListenergetInstance()
Returns a DigitsKeyListener that accepts the digits 0 through 9.

        return getInstance(false, false);
    
public static android.text.method.DigitsKeyListenergetInstance(boolean sign, boolean decimal)
Returns a DigitsKeyListener that accepts the digits 0 through 9, plus the minus sign (only at the beginning) and/or decimal point (only one per field) if specified.

        int kind = (sign ? SIGN : 0) | (decimal ? DECIMAL : 0);

        if (sInstance[kind] != null)
            return sInstance[kind];

        sInstance[kind] = new DigitsKeyListener(sign, decimal);
        return sInstance[kind];
    
public static android.text.method.DigitsKeyListenergetInstance(java.lang.String accepted)
Returns a DigitsKeyListener that accepts only the characters that appear in the specified String. Note that not all characters may be available on every keyboard.

        // TODO: do we need a cache of these to avoid allocating?

        DigitsKeyListener dim = new DigitsKeyListener();

        dim.mAccepted = new char[accepted.length()];
        accepted.getChars(0, accepted.length(), dim.mAccepted, 0);

        return dim;
    
private static booleanisDecimalPointChar(char c)

        return c == '.";
    
private static booleanisSignChar(char c)


          
        return c == '-" || c == '+";