当视图失去焦点时,如何在EditText中掩盖文本。

4

如果用户输入"1234",他们将在EditText字段中看到"1234"。但当该字段失去焦点时,我希望它显示为 "****"。

所以,我实现了一个自定义的TransformationMethod,只有在EditText字段没有焦点时才会掩盖输入的文本。

当我输入文本"12345"时,它正常显示为"12345",但是当我点击其他字段时,数字永远不会被掩盖。我想看到"*****",但仍然看到同样的"12345"。

然而,如果我旋转设备(强制重新加载所有内容),它就会正确显示"*****"。当我点击EditText字段时,它会正确地将掩盖的文本从"*****"更改为"12345"。因此,在获得焦点时它有效,但在失去焦点时无效。我尝试了实现OnFocusChangeListener,但似乎没有影响。

有没有办法在EditText字段失去焦点时强制重新绘制文本?

设置:

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

CustomPasswordTransformationMethod: public class CustomPasswordTransformationMethod extends PasswordTransformationMethod { // 用于混淆密码的数字 private int unObfuscated = 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
        }
    }
};
2个回答

2

试一下,看看它是否满足您的需求。

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

我知道我们应该避免“谢谢”评论,但是真的非常感谢! - Dan Anderson

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);
        }
    }
});

这是一个很好的想法,但不幸的是,由于每次进行编辑时我们都会将整个字符串保存到数据库中,因此我需要保持底层文本不变。使用密码TransformationMethod可以在将其作为可编辑内容传递给TextWatcher时保持底层文本为“12345”。 - Dan Anderson

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接