Android开发之EditText字符串过滤器InputFilter

本博客地址:http://blog.csdn.net/talentclass_ctt/article/details/54773340

一、InputFilter 是什么?有什么用?

在Android编程中,一般没什么特殊要求的话,我们用EditText可能只会用到xml里面的限制输入类型、最大长度,行数等等这些简单的控制,但如果我们想要进行一些复杂一点的输入控制(比如:限制单字节多少位,双字节多少位;限制只能输入业务要求的特定的字符;修改输入字符)就要用今天要讲的InputFilter了。

二、怎么用?
1、创建一个类去implements InputFilter,实现filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend)方法;
2、在要用到的地方调用EditText的setFilters(InputFilter[] filters)方法即可。
例子(限制双字节字符16位,单字节字符32位):

NameFilter.java

public class NameFilter implements InputFilter {

    private int mMaxLen = 16;

    public NameFilter() {
    }

    public NameFilter(int maxLen) {
        this.mMaxLen = maxLen;
    }

    @Override
    public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
        int dindex = 0;
        int count = 0; // 判断是否到达最大长度

        while (count <= mMaxLen && dindex < dest.length()) {
            char c = dest.charAt(dindex++);
            if (c < 128) {// 按ASCII码表0-127算
                count = count + 1;
            } else {
                count = count + 2;
            }
        }

        if (count > mMaxLen) {
            return dest.subSequence(0, dindex - 1);
        }

        int sindex = 0;
        while (count <= mMaxLen && sindex < source.length()) {
            char c = source.charAt(sindex++);
            if (c < 128) {
                count = count + 1;
            } else {
                count = count + 2;
            }
        }

        if (count > mMaxLen) {
            sindex--;
        }
        return source.subSequence(0, sindex);
    }
}
在要用到的地方加上

mEtName.setFilters(new InputFilter[]{new NameFilter(16)});// 表示单字节字符最长16位,双字节字符最长8位;根据业务传值
三、更多
LoginFilter实现了InputFilter接口,而它的构造函数是不公开的,所以我们可以通过重写LoginFilter的子类的isAllowed方法,达到代码过滤EditText字符串的目的。

LoginFilter.java

public abstract class LoginFilter implements InputFilter {
    LoginFilter() {
        throw new RuntimeException("Stub!");
    }

    public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
        throw new RuntimeException("Stub!");
    }

    public void onStart() {
        throw new RuntimeException("Stub!");
    }

    public void onInvalidCharacter(char c) {
        throw new RuntimeException("Stub!");
    }

    public void onStop() {
        throw new RuntimeException("Stub!");
    }

    public abstract boolean isAllowed(char var1);

    public static class PasswordFilterGMail extends LoginFilter {
        public PasswordFilterGMail() {
            throw new RuntimeException("Stub!");
        }

        public PasswordFilterGMail(boolean appendInvalid) {
            throw new RuntimeException("Stub!");
        }

        public boolean isAllowed(char c) {
            throw new RuntimeException("Stub!");
        }
    }

    public static class UsernameFilterGeneric extends LoginFilter {
        public UsernameFilterGeneric() {
            throw new RuntimeException("Stub!");
        }

        public UsernameFilterGeneric(boolean appendInvalid) {
            throw new RuntimeException("Stub!");
        }

        public boolean isAllowed(char c) {
            throw new RuntimeException("Stub!");
        }
    }

    public static class UsernameFilterGMail extends LoginFilter {
        public UsernameFilterGMail() {
            throw new RuntimeException("Stub!");
        }

        public UsernameFilterGMail(boolean appendInvalid) {
            throw new RuntimeException("Stub!");
        }

        public boolean isAllowed(char c) {
            throw new RuntimeException("Stub!");
        }
    }
}
可以看到继承LoginFilter的有PasswordFilterGMail、UsernameFilterGeneric、UsernameFilterGMail,我们无法继承LoginFilter,但是可以继承这三个类。
例子(限制只能输入数字和英文):

MyLoginFilter.java

public class MyLoginFilter extends LoginFilter.UsernameFilterGMail {
    public  MyLoginFilter() {
        super();
    }

    @Override
    public boolean isAllowed(char c) {
        if ('0' <= c && c <= '9')
            return true;
        if ('a' <= c && c <= 'z')
            return true;
        if ('A' <= c && c <= 'Z')
            return true;
        return false;
    }
}
原创作品,转载请注明:  http://blog.csdn.net/talentclass_ctt/article/details/54773340




你可能感兴趣的:(Android)