問題描述
我試圖用 asterisk (*)
符號代替 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?
謝謝
推薦答案
我覺得你應該通過文檔.創建你的 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中的點,輸入類型為textPassword?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!