我有一个应用程序,人们需要使用密码登录。我只想显示最后输入的字符,但我似乎得到的只是所有字符点或所有可见字符。
我尝试了一些事情:
password.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);
password.setTransformationMethod(PasswordTransformationMethod.getInstance());
password.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
并在xml中设置inputtype。
我只在 galaxy s2 上测试过,因为这是我办公室目前唯一的安卓设备,所以我不知道问题是否只出在我的设备上。
编辑: 刚刚在一位同事的 HTC Sensation 上进行了测试,它确实可以在他的手机上正常工作,但问题仍然是如何在 Galaxy S2 上获得同样的功能?
最佳答案
这个问题被问到已经将近 1.5 年了 :P。但是我有同样的要求,并且成功地实现了它。将 MyTransformation 类的对象作为参数传递到 setTransformationMethod 中,您就可以开始了:)这是代码。
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) {
//This is the check which makes sure the last character is shown
if(index != mSource.length()-1)
return '•';
else
return mSource.charAt(index);
}
public int length() {
return mSource.length(); // Return default
}
public CharSequence subSequence(int start, int end) {
return mSource.subSequence(start, end); // Return default
}
}
}
关于android - 在 EditText 中显示密码的最后一个字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8601088/