editText inputType密码中的自定义项目符号

时间:2015-09-21 13:43:36

标签: android android-edittext android-inputtype

我需要为editText创建密码的自定义项目符号,并在项目符号之间添加一些填充。

有没有办法做到这一点?

1 个答案:

答案 0 :(得分:1)

要更改显示的字符,您可以调用方法setTransformationMethod (TransformationMethod method)并将其传递给自定义PasswordTransformationMethod

这可能如下所示:

public class AsteriskPasswordTransformationMethod extends PasswordTransformationMethod {
    @Override
    public CharSequence getTransformation(CharSequence source, View view) {
        return new PasswordCharSequence(source);
    }
    private class PasswordCharSequence implements CharSequence {
        private CharSequence mSource;
        public PasswordCharSequence(CharSequence source) {
            mSource = source; // Store char sequence
        }
        public char charAt(int index) {
            return '*'; // This is the important part
        }
        public int length() {
            return mSource.length(); // Return default
        }
        public CharSequence subSequence(int start, int end) {
            return mSource.subSequence(start, end); // Return default
        }
    }
};

然后像这样设置:

textView.setTransformationMethod(new AsteriskPasswordTransformationMethod());

Source

要更改填充等,您应该使用textScaleX,或者如果您处于API Level 21以上,请使用XML中的letterSpacing