在Android中如何显示星号(*)代替点的EditText上有inputtype为textPassword? [英] In android how to show asterisk (*) in place of dots in EditText having inputtype as textPassword?
问题描述
我想在星号(*)
到位点的符号来显示的EditText
为 inputType
为 textPassword
。我碰到职位,要求使用 setTransformationMethod()
和实施 PasswordTransformationMethod
。但我这个类的,我需要实现以及如何显示星号哪种方法?有没有其他办法可以做到这一点?
I am trying to show in asterisk (*)
symbol in place of dots in EditText
having inputType
as textPassword
. I came across post that ask to use setTransformationMethod()
and implement PasswordTransformationMethod
. But which method I of that class need I implement and how show asterisk? Is there other way to do that?
感谢
推荐答案
我觉得你应该通过<一个href="http://developer.android.com/reference/android/text/method/PasswordTransformationMethod.html#getTransformation%28java.lang.CharSequence,%20android.view.View%29">the文档。创建您的 PasswordTransformationMethod
类,并在 getTransformation()
的方法,只返回的字符串*
字符的长度相同的密码字段的内容。
I think you should go through the documentation. Create your PasswordTransformationMethod
class, and in the getTransformation()
method, just return a string of *
characters that is the same length as the contents of your password field.
我做了一些摆弄,并提出了为我工作的,使现场充满了 *
秒的匿名类。我将它转换成可用的类在这里:
I did some fiddling and came up with an anonymous class that worked for me to make a field full of *
s. I converted it into a usable class here:
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
}
}
};
// Call the above class using this:
text.setTransformationMethod(new MyPasswordTransformationMethod());
这篇关于在Android中如何显示星号(*)代替点的EditText上有inputtype为textPassword?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!