Display last password character in EditText

I have an application where people must log in with a password. I would like only the last character to be entered for display, but all I seem to get is all dots with characters or all visible characters.

I have tried several things:

password.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);
password.setTransformationMethod(PasswordTransformationMethod.getInstance());
password.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);

and setting the input type in xml.

I tested only on the galaxy s2, since this is the only Android device in my office at the moment, so I do not know if there is a problem only with my device.

edit: It’s just tested on HTC Sensation from a colleague, and it works as intended on his phone, but the question remains, how to get the same thing on the Galaxy S2?

+6
source share
3

, : P. , . MyTransformation setTransformationMethod, :) .

public class MyTransformation 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) {
        //This is the check which makes sure the last character is shown
            if(index != mSource.length()-1)
                return '•';
            else
                return mSource.charAt(index);
        }
        public int length() {
            return mSource.length(); // Return default
        }
        public CharSequence subSequence(int start, int end) {
            return mSource.subSequence(start, end); // Return default
        }
    }
}

enter image description here

+16

AndyFaizan, Backspace. () , .

public class CustomPasswordTransformation extends PasswordTransformationMethod {
    boolean lastActionWasDelete = false;

    @Override
    public CharSequence getTransformation(CharSequence source, View view) {
        return new PasswordCharSequence(source);
    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        super.onTextChanged(s, start, before, count);
        this.lastActionWasDelete = before > count;
    }

    private class PasswordCharSequence implements CharSequence {
        private CharSequence source;

        PasswordCharSequence(CharSequence source) {
            this.source = source;
        }

        public char charAt(int index) {
            //This is the check which makes sure the last character is shown
            if (!lastActionWasDelete && index == source.length() - 1) return source.charAt(index);
            return '•';
        }

        public int length() {
            return source.length(); // Return default
        }

        public CharSequence subSequence(int start, int end) {
            return source.subSequence(start, end); // Return default
        }
    }
}
0

, .

,

password.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);

.

. , , " ".

If you have to ignore the system-wide settings, the decision to convert passwords AndyFaizan post- remains faithful .

0
source

All Articles