当前位置: 首页 > 知识库问答 >
问题:

视图失去焦点时如何屏蔽EditText中的文本。

糜昌胤
2023-03-14

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

因此,我实现了一个自定义TransformationMethod,它只会在EditText字段没有焦点时屏蔽输入的文本。

当我输入文本“12345”时,它会显示它应该“12345”,但当我单击不同的字段时,数字永远不会被掩盖。我想看 "*****" 但我仍然看到相同的“12345”

但是,如果我旋转设备(强制它重新加载所有内容),它会正确显示 "*****". 当我单击EditText字段时,它会正确地将屏蔽文本从 "*****" 更改为“12345”,因此它在获得焦点时有效,但在失去焦点时无效。我尝试过实现OnFocus usChangeListener,但似乎没有影响。

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

设置:

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

CustomPasswordTransformationMethod:公共类CustomPasswordTransformationMethod扩展了PasswordTransformationMethod{private int unobuscated=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个答案

杜君浩
2023-03-14

也许你可以试着保持简单:

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);
        }
    }
});
微生弘
2023-03-14

试试这个,看看它是否能满足你的需要。

editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
    @Override
    public void onFocusChange(View view, boolean hasFocus) {
        if(hasFocus){
            editText.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
        }
        else{
            editText.setTransformationMethod(PasswordTransformationMethod.getInstance());
        }
    }
});
 类似资料:
  • 当失去焦点时,我需要捕捉,我搜索了其他问题,但没有找到答案。 我这样使用 但是,这对我不起作用。

  • 我希望能够检测编辑文本何时没有被点击,这样我就可以用文本视图替换它。我试过了 但是祝酒词不会被调用。

  • 在我的活动中,我有一个editText字段。当用户点击它时,editText获得焦点并出现键盘。现在,当用户按下手机上的硬件后退按钮时,键盘会消失,但光标仍保留在Edittext中,即它仍然具有焦点。当按下后退按钮时,是否有可能使EditText失去焦点?我尝试使用以下代码,但它不起作用:

  • 在我的应用程序中,我需要能够捕捉到编辑文本(editText)是否失去了焦点,我已经在java中找到了如何做到这一点,但在kotlin中我还不能找到如何做到这一点。 我找到的Java答案是: 有人能帮我把这个转换成kotlin吗?

  • 我有列表视图和一些列“ID”、“产品名称”和“供应商”。我可以添加、编辑或删除这些项目,在“搞乱”项目列表后,会刷新类似这样的视图。 所以这个方法工作得很好,除了每次我刷新列表的时候,项目的焦点都丢失了,这是合乎逻辑的,因为我从列表中删除了所有的项目,并再次填充它。所以我的问题是,我如何才能保持对编辑和新添加的项目的关注,主要是当我编辑一些项目时,我希望listview不要滚动到顶部,而是停留在编

  • 我正在一个活动的登录页面上工作,其中用户名和密码作为两个编辑文本框。当焦点从用户名更改为密码框时,我试图显示Toast消息:-如果在用户名框中输入的文本中有空格-如果没有空格,则不显示Toast。