[英]How to mask text in EditText when the view loses focus.
So if a user enters "1234" they will see "1234" in the EditText field. 因此,如果用户输入“ 1234”,则他们将在EditText字段中看到“ 1234”。 but when that field loses focus I want it to show "****"
但是当该字段失去焦点时,我希望它显示“ ****”
So I've implemented a custom TransformationMethod that will only mask the entered text if the EditText field does not have focus. 因此,我实现了一个自定义的TransformationMethod,该方法仅在EditText字段没有焦点时才掩盖输入的文本。
when I enter the text "12345" it shows it as it should "12345" but when I click on a different field the numbers never get masked. 当我输入文本“ 12345”时,它应显示为应为“ 12345”,但是当我单击其他字段时,数字永远不会被掩盖。 I want to see "*****" but I still see the same "12345"
我想看到“ *****”,但仍然看到相同的“ 12345”
If I rotate the device though (force it to reload everything) it correctly shows "*****". 如果我旋转设备(强迫它重新加载所有内容),它会正确显示“ *****”。 And when I click on the EditText Field it correctly changes the masked text from "*****" to "12345" So it works when gaining focus but not when losing focus.
而且,当我单击EditText字段时,它可以将遮罩的文本正确地从“ *****”更改为“ 12345”,因此在获得焦点时有效,而在失去焦点时则无效。 I've tried implementing an OnFocusChangeListener but it seems to have no affect.
我试过实现OnFocusChangeListener,但似乎没有任何效果。
Is there any way I can force the EditText Field to redraw the text when it loses focus? 有什么办法可以迫使EditText字段在失去焦点时重新绘制文本?
SetUp: 设定:
editText.setTransformationMethod(CustomPasswordTransformationMethod(numUnobfuscatedDigits))
editText.setOnFocusChangeListener { view, hasFocus ->
((EditText)view).invalidate()
((EditText)view).refreshDrawableState()
CustomPasswordTransformationMethod: public class CustomPasswordTransformationMethod extends PasswordTransformationMethod { private int unObfuscated = 1; CustomPasswordTransformationMethod:公共类CustomPasswordTransformationMethod扩展了PasswordTransformationMethod {private int unObfuscated = 1; private boolean mIsFocused = false;
私有布尔值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
}
}
};
Try this and see if it does what you need. 试试这个,看看它是否满足您的需求。
editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View view, boolean hasFocus) {
if(hasFocus){
editText.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
}
else{
editText.setTransformationMethod(PasswordTransformationMethod.getInstance());
}
}
});
Maybe you can try to keep it simple: 也许您可以尝试保持简单:
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);
}
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.