Android开发中,为了安全,输入密码时隐藏输入的字符,但往往会有回显一下输入的字符,然后才变为 * ,下面是解决这种问题的方法:
写一个类继承PasswordTransformationMethod
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) {
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
}
}
}
然后在代码中给EditText设置:
etPassword.setTransformationMethod(new MyTransformation());
这样问题完美解决