因此,如果用戶輸入「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
}
}
};
我知道我們應該避免「謝謝」意見,但認真謝謝你! –