在密码模式下更改TextInput中的字符

时间:2009-09-02 11:39:44

标签: flex

我只是对flex中的TextInput控件的密码模式显示感到好奇。当'displayAsPassword'设置为true时,控件显示星号而不是字符本身。引用文档,

  

displayAsPassword
  如果为true,则该字段不显示输入的文本,而是输入控件的每个文本字符显示为字符“*”。

那么,有没有办法更改此显示的字符,除非当然创建自定义组件或扩展现有的TextInput控件?

非常感谢。

2 个答案:

答案 0 :(得分:3)

只是一种解决方法


适用于Flex 4.6

根据TextInput组件创建一个新皮肤," skin_password"或类似的东西:

New MXML Skin

现在,在皮肤内部,搜索一下(有些编辑可以按 CTRL + O ,写 textDisplay 并按输入):

<s:RichEditableText id="textDisplay" left="1" right="1" top="1" bottom="1"
                    verticalAlign="middle" widthInChars="12"
                    left.normal="1" right.normal="1" top.normal="1" bottom.normal="1"/>

添加属性 displayAsPassword = true

<s:RichEditableText id="textDisplay" left="1" right="1" top="1" bottom="1"
                    displayAsPassword="true" verticalAlign="middle" widthInChars="12"
                    left.normal="1" right.normal="1" top.normal="1" bottom.normal="1"/>

搜索此方法:

override protected function initializationComplete():void
    {
        useChromeColor = true;
        super.initializationComplete();
    }

附加此行(在我的情况下,黑点●而不是默认的星号*):

textDisplay.mx_internal::passwordChar = "●";

现在,只需将skinClass属性设置为TextInput组件(我的皮肤示例位于包皮肤 中):

<s:TextInput id="tiPassword" height="30" maxChars="12" skinClass="skins.skin_password" />

现在你有这样的事情: see working

-

-

多一点解释


当您将 displayAsPassword 设置为 true 时,Flex会将textFlow添加到TextInput中包含的RichEditableText,并使用相同数量的&#34;密码字符&#34;

您可以在 RichEditableText.as 的代码中看到这一点:

public function get text():String 
    {
        // Note: if displayAsPassword, _text will contain the actual text and the text flow will
        // contain the same number of passwordChars.

        // Go to the source if there isn't a pending change.  getText has its own buffering and 
        // only extracts the text from the TextFlow when it is damaged. 
        if (_textContainerManager && !textChanged && !textFlowChanged && !contentChanged && !displayAsPassword)
            return _textContainerManager.getText("\n");

        // Extracting the plaintext from a TextFlow is somewhat expensive,
        // as it involves iterating over the leaf FlowElements in the TextFlow.
        // Therefore we do this extraction only when necessary, namely when
        // you first set the 'content' or the 'textFlow'
        // (or mutate the TextFlow), and then get the 'text'.
        if (_text == null)
        {
            // If 'content' was last set,
            // we have to first turn that into a TextFlow.
            if (_content != null)
                _textFlow = createTextFlowFromContent(_content);

            // Once we have a TextFlow, we can export its plain text.
            _text = staticPlainTextExporter.export(
                _textFlow, ConversionType.STRING_TYPE) as String;
        }

        return _text;
    }

在函数 textContainerManager_flowOperationBeginHandler(event:FlowOperationEvent)中:void

if (_displayAsPassword)
            {
                // Remove deleted text.
                if (delLen > 0)
                {
                    _text = splice(_text, delSelOp.absoluteStart, 
                                   delSelOp.absoluteEnd, "");                                    
                }

                // Add in the inserted text.
                if (textToInsert.length > 0)
                {
                    _text = splice(_text, insertTextOperation.absoluteStart,
                        insertTextOperation.absoluteStart, textToInsert);

                    // Display the passwordChar rather than the actual text.
                    textToInsert = StringUtil.repeat(passwordChar,
                        textToInsert.length);
                }
            }

您可以看到 passwordChar 变量,可以使用 mx_internal

访问该变量
 /**
     *  @private
     */
    mx_internal var passwordChar:String = "*";

答案 1 :(得分:1)

This thread提供了一些建议。最简单的方法是找到一个Shift-8(星号)是你想要的字形的字体。