Custom bullet in editText inputType password

2019-09-08 15:18发布

问题:

I need to create custom bullet for password for editText and put some padding between bullet symbols.

Is there some way to do this?

回答1:

For changing the character that is displayed you can call the method setTransformationMethod (TransformationMethod method) and pass it a custom PasswordTransformationMethod.

This could look something like the following:

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
        }
    }
};

and then set it like this:

textView.setTransformationMethod(new AsteriskPasswordTransformationMethod());

Source

For changing the padding and such you should use either use textScaleX or if you are at API Level 21+ use letterSpacing in the XML.