Is it possible to replace dots in password (EditText) with custom view?

给你一囗甜甜゛ 提交于 2020-01-17 05:09:08

问题


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());



来源:https://stackoverflow.com/questions/29891797/is-it-possible-to-replace-dots-in-password-edittext-with-custom-view

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