Is it possible to replace dots in password (EditTe

2019-09-16 19:25发布

问题:

I want to replace dots in EditText (android:inputType="textPassword") with custom xml or image. I can replace dots with any other symbol using PasswordTransformationMethod method, but this is not what is required in this case.

So, is it possible?

Thanks.

回答1:

There is Span API for Edit text. You could use TextWatcher in connection with ImageSpan. Also Don't forget about copy/paste functionalty in secure reson. So solution is listening text input, and wrap it with ImageSpans. when you want to get password you will be able simple use EditText.getText.toString();



回答2:

Please look at this http://developer.android.com/reference/android/text/method/PasswordTransformationMethod.html

Try this,

public class MyPasswordTransformationMethod 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
    }
}

}; text.setTransformationMethod(new MyPasswordTransformationMethod());