设置EditText输入的文字全部变成大写或小写方案总结

1.通过setFilters方法,设置输入变大写

edittext.setFilters(new InputFilter[] {new InputFilter.AllCaps()});

2.通过setTransformationMethod方法设置输入变大写

public class AllCapTransformationMethod extends ReplacementTransformationMethod {

    private char[] lower = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
    private char[] upper = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
    private boolean allUpper = false;

    public AllCapTransformationMethod(boolean needUpper) {
        this.allUpper = needUpper;
    }

    @Override
    protected char[] getOriginal() {
        if (allUpper) {
            return lower;
        } else {
            return upper;
        }
    }

    @Override
    protected char[] getReplacement() {
        if (allUpper) {
            return upper;
        } else {
            return lower;
        }
    }
}

到时要使用的时候,就给EditText设好参数就可以了,像下面这样就可以自动变大写了.false就是小写

editText.setTransformationMethod(new AllCapTransformationMethod(true));

猜你喜欢

转载自blog.csdn.net/u011118524/article/details/93738542