Custom bullet in editText inputType password

2019-09-08 14:55发布

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条回答
一纸荒年 Trace。
2楼-- · 2019-09-08 15:39

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.

查看更多
登录 后发表回答