Can you mask EditText values on a char per char basis?
I have an EditText where I want to mask character values but hyphens. For example "11-11" would ideally display as "**-**".
I know if you define android:password="true" in your EditText configuration, it will mask all the characters (except the last entered value). Is there a way to override that functionality to display hyphens in clear text?
开发者_如何学运维*edit - Moved answer out of question...
I figured this out if anyone in the future wants to know how to do it. I extended PasswordTransformationMethod, and overrode getTransformation(). Unfortunately, the authors of that class did not make it the slightest bit extensible, so I ended up having to copy in all the code for the private classes PasswordCharSequence and Visible.
The actual change to allow hyphens was simple. I replaced:
if (!visible) {
dest[i - start + off] = DOT;
}
with:
if (!visible && dest[i - start + off] != '-') {
dest[i - start + off] = DOT;
}
Hope that helps someone. Also, I'd be interested if someone finds a cleaner solution to this problem.
*Edit - Actually, I just noticed that the masking doesn't work in landscape orientation... I'll update this when I figure it out.
精彩评论