Put a space after transforming the password text into asterisks

本小妞迷上赌 提交于 2021-01-28 07:10:53

问题


In my application, I am using the below passwordTransformationClass for showing the password in asterisk format. The problem is, I want a space after each text in the edit text, i.e. a space after every asterisk. But the overridden methods don’t take space except a single asterisk character.

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

回答1:


Input : <*****>

I hope this is your input in EditTextView and you want to transform the EditTextView text to something like

Output: <* * * * *>

then set some custom EditTextView fonts which have much spacing between characters...

Change font for EditText in Android?

and if you are going through Editext.addTextChangedListener, we can do it but we have to write down some expensive Logics into it to handle copy, paste, backspace and so many other occurrences .. :)




回答2:


Try to change the code from

        public char charAt(int index)
        {
            return '*'; // This is the important part
        }

to

        public string charAt(int index)
        {
            return "* "; // This is the important part
        }


来源:https://stackoverflow.com/questions/38197773/put-a-space-after-transforming-the-password-text-into-asterisks

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!