2017-10-04 11 views
2

그래서 사용자가 "1234"를 입력하면 EditText 필드에 "1234"가 표시됩니다. 그 필드가 포커스를 잃었을 때 "****"을 표시하고 싶습니다보기가 포커스를 잃을 때 EditText에서 텍스트를 마스크하는 방법.

EditText 필드에 포커스가없는 경우 입력 된 텍스트 만 마스크하는 사용자 정의 TransformationMethod를 구현했습니다.

"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 부울 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에 Editable로서 건네 받으면 (자), 기본이되는 텍스트 "12345"를 보관 유지합니다. –