ActionScript 3.0 文本输入和显示

发布于 2024-11-16 06:48:26 字数 601 浏览 3 评论 0原文

我是flash新手,下面是我的脚本,我有3个文本输入框,name1,name2,name3和3个动态文本,output1,output2,output3。用户在框中输入文本后,它应该在动态输出文本中显示完全相同。它适用于第一个,但不适用于第二个和第三个。我以不同的方式重命名了更改处理程序以消除编译错误,但现在只有第一个有效。如果我想要多个文本框主菜,有更好的方法吗?

name1.addEventListener(Event.CHANGE, changeHandler);

function changeHandler(e:Event):void 
{
    output1.text = name1.text

}
name2.addEventListener(Event.CHANGE, changeHandler);

function changeHandler1(e:Event):void 
{
    output2.text = name2.text;
}

name3.addEventListener(Event.CHANGE, changeHandler);

function changeHandler2(e:Event):void 
{
    output3.text = name3.text;
}

I am new at flash, the below is my script, i have 3 textinput boxes, name1, name2, name3 and 3 dynamic texts, output1, output2, output3. Once the user, enters the text in the box, it should appear exactly the same in the dynamic output text. It works for the first one, but does not work for the second and third one. I renamed changehandlers differently to remove compile errors, but now only the first one works. Is there a better way of doing this, if i want to have multiple textbox entrees?

name1.addEventListener(Event.CHANGE, changeHandler);

function changeHandler(e:Event):void 
{
    output1.text = name1.text

}
name2.addEventListener(Event.CHANGE, changeHandler);

function changeHandler1(e:Event):void 
{
    output2.text = name2.text;
}

name3.addEventListener(Event.CHANGE, changeHandler);

function changeHandler2(e:Event):void 
{
    output3.text = name3.text;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清晨说晚安 2024-11-23 06:48:27

您忘记在后两个 addEventListener() 调用中更改侦听器函数的名称。它当前对所有三个事件调用 changeHandler()

您应该:

name2.addEventListener(Event.CHANGE, changeHandler1);
name3.addEventListener(Event.CHANGE, changeHandler2);

您可以创建一个类来管理连接输入文本字段与输出文本字段:

package
{
    import flash.text.TextField;
    import flash.events.Event;

    public class TextBinder extends Object
    {
        // vars
        private var _input:TextField;
        private var _output:TextField;

        /**
         * Joins input with output
         * @param inp The input text field
         * @param outp The output text field
         */
        public function join(inp:TextField, outp:TextField):void
        {
            _input = inp;
            _output = outp;

            _input.addEventListener(Event.CHANGE, _change);
        }

        /**
         * Event.CHANGE
         */
        private function _change(e:Event):void
        {
            _output.text = _input.text;
        }
    }
}

现在您可以循环遍历文本字段并使用以下方法连接它们:

var tb:TextBinder = new TextBinder();
tb.join(name1, output1);

You forgot to change the name of the listener functions in the latter two addEventListener() calls. It currently calls changeHandler() on all three events.

You should have:

name2.addEventListener(Event.CHANGE, changeHandler1);
name3.addEventListener(Event.CHANGE, changeHandler2);

You can create a class that manages joining the input text field with the output text field:

package
{
    import flash.text.TextField;
    import flash.events.Event;

    public class TextBinder extends Object
    {
        // vars
        private var _input:TextField;
        private var _output:TextField;

        /**
         * Joins input with output
         * @param inp The input text field
         * @param outp The output text field
         */
        public function join(inp:TextField, outp:TextField):void
        {
            _input = inp;
            _output = outp;

            _input.addEventListener(Event.CHANGE, _change);
        }

        /**
         * Event.CHANGE
         */
        private function _change(e:Event):void
        {
            _output.text = _input.text;
        }
    }
}

And now you can loop through your text fields and join them using this:

var tb:TextBinder = new TextBinder();
tb.join(name1, output1);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文