2011-05-25 210 views
4

我正在處理需要使用密碼字段(即編輯文本)的任務,以使用星號(*)而不是點(。)來隱藏用戶輸入。目前它顯示爲點。 請告訴我如果可能使用android的本地方法來做到這一點。或者,如果任何人已經這樣做,請發佈代碼。如何將密碼字段更改爲星號而不是點

在此先感謝..

+0

請問爲什麼? – Jonas 2011-05-25 14:10:00

+0

它是一個必需品喬納斯。 – Sachin 2011-05-25 14:20:07

回答

1

我會想象你可以重寫監聽器類方法來修改要顯示的文字,以便它讀作「*」,但保留在後臺的實際字符串的地方。因此,每個用戶輸入一個字母時,你把它顯示的字符串與添加到您的累計「密碼」字符串,而是替換字符*

2
public final void setTransformationMethod (TransformationMethod method) 

Since: API Level 1 
Sets the transformation that is applied to the text that this TextView is displaying. 
Related XML Attributes 

android:password 
android:singleLine 

允許你改變任何字符

3

非常遲到的答案,我相信你不再在意,但其他人可能會。

初始化EditText字段。

EditText UPL =(EditText) findViewById(R.id.UserPasswordToLogin) ; 
    UPL.setTransformationMethod(new AsteriskPasswordTransformationMethod()); 

然後創建一個新的Java類,名爲AsteriskPasswordTransformationMethod.java延伸PasswordTransformationMethod

這裏是代碼:

import android.text.method.PasswordTransformationMethod; 
import android.view.View; 

public class AsteriskPasswordTransformationMethod 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 
     } 
    } 
}; 
+2

應該接受答案 – zirael 2015-01-23 14:30:24

相關問題