0

我正在尝试根据用户输入将输入文本行为从“单行”更改为“密码”并返回,这样当用户输入不正确的密码时,文本字段将变成带有文本“密码不正确”的单行文本框”。然后,一旦他们再次开始输入,文本框将再次充当密码类型文本框。

4

1 回答 1

2

这是您想要的东西,它会显示Incorrect PasswordTextField输入正确的密码之前。

package 
{
    public class Main extends Sprite 
    {
        private var tf:TextField = new TextField();

        public function Main():void 
        {
            if (stage) init();
            else addEventListener(Event.ADDED_TO_STAGE, init);
        }

        private function init(e:Event = null):void 
        {
            removeEventListener(Event.ADDED_TO_STAGE, init);


            tf.border = true;
            tf.displayAsPassword = true;
            tf.multiline = false;
            tf.height = 20;
            tf.type = TextFieldType.INPUT;
            tf.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
            tf.addEventListener(FocusEvent.FOCUS_IN, onFocusIn);

            addChild(tf);

        }

        private function onFocusIn(e:FocusEvent):void 
        {
            tf.text = "";
            tf.displayAsPassword = true;
        }

        private function onKeyDown(e:KeyboardEvent):void 
        {
            if (e.keyCode == Keyboard.ENTER)
            {
                if (tf.text == "pass")
                {
                    trace("logged in");
                }
                else
                {
                    tf.displayAsPassword = false;
                    tf.text = "Incorrect Password";
                    stage.focus = stage;
                }
            }
        }

    }

}
于 2013-04-24T00:08:23.757 回答