2017-10-04 65 views
2

因此,如果用戶輸入「1234」,他們將在EditText字段中看到「1234」。但是當該字段丟失焦點時,我希望它顯示「****」如何在視圖失去焦點時在EditText中掩蓋文本。

所以我實現了一個自定義TransformationMethod,它只會在EditText字段沒有焦點時屏蔽輸入的文本。

當我輸入文本「12345」它顯示它應該是「12345」,但當我點擊不同的字段時,數字永遠不會被屏蔽。我想看到「*****」,但我仍然看到相同的「12345」

如果我旋轉設備(強制它重新加載所有內容),它會正確顯示「*****」。當我點擊EditText字段時,它將屏蔽的文本從「*****」正確地更改爲「12345」。因此,它在獲得焦點時起作用,但在失去焦點時不起作用。我試過實現一個OnFocusChangeListener,但它似乎沒有任何影響。

有什麼辦法可以強制EditText字段重新繪製文本時,它失去了焦點?

設置:

editText.setTransformationMethod(CustomPasswordTransformationMethod(numUnobfuscatedDigits)) 
editText.setOnFocusChangeListener { view, hasFocus -> 
        ((EditText)view).invalidate()  
        ((EditText)view).refreshDrawableState()      

CustomPasswordTransformationMethod: 公共類CustomPasswordTransformationMethod延伸PasswordTransformationMethod { 私人INT是非模糊= 1; private boolean mIsFocused = false;

/** 
    * @param number the number of digits that will be unObfuscated at the end of the input string. Must be a positive integer or 0. 
    */ 
    public CustomPasswordTransformationMethod(int number) { 
     if (number < 0) { 
      Log.e(TAG, "Invalid parameter number =" + number + " number of un-obfuscated digits must be a positive integer or 0."); 
      unObfuscated = 0; 
     } 
     unObfuscated = number; 
    } 

    @Override 
    public CharSequence getTransformation(CharSequence source, View view) { 
     return new PasswordCharSequence(source); 
    } 

    @Override 
    public void onFocusChanged(View view, CharSequence sourceText, 
           boolean focused, int direction, 
           Rect previouslyFocusedRect) { 
     super.onFocusChanged(view,sourceText,focused, direction, previouslyFocusedRect); 
     mIsFocused = focused; 
    } 


    private class PasswordCharSequence implements CharSequence { 
     private CharSequence mSource; 

     public PasswordCharSequence(CharSequence source) { 
      mSource = source; // Store char sequence 
     } 

     public char charAt(int index) { 
      if(mIsFocused) return mSource.charAt(index); 
      else { 
       if (index < ((length()) - unObfuscated)) return '●'; 
       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 
     } 
    } 
}; 

回答

1

試試這個,看看它做你需要什麼。

editText.setOnFocusChangeListener(new View.OnFocusChangeListener() { 
    @Override 
    public void onFocusChange(View view, boolean hasFocus) { 
     if(hasFocus){ 
      editText.setTransformationMethod(HideReturnsTransformationMethod.getInstance()); 
     } 
     else{ 
      editText.setTransformationMethod(PasswordTransformationMethod.getInstance()); 
     } 
    } 
}); 
+0

我知道我們應該避免「謝謝」意見,但認真謝謝你! –

0

也許你可以儘量保持簡單:

String password = ""; 

editText.setOnFocusChangeListener(new OnFocusChangeListener() { 
    @Override 
    public void onFocusChange(View view, boolean hasFocus) { 
     if (hasFocus) { 
      editText.setText(password, TextView.BufferType.EDITABLE); 
     } else { 
      password = editText.getText().toString(); 
      String ofuscated = ""; 
      for (int i = 0; i < password.length(); i++){ ofuscated += "*"; } 
      editText.setText(ofuscated, TextView.BufferType.EDITABLE); 
     } 
    } 
}); 
+0

這是一個好主意,不幸的是我需要底層文本保持不變,因爲每次編輯時都會將整個字符串保存到數據庫中。使用密碼TransformationMethod在作爲可編輯傳遞給TextWatcher時將底層文本「12345」保留。 –