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を拡張します。 プライベート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

これを試してみて、それが何が必要ないかどうかを確認します。

+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"を保持します。 –

関連する問題