Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/apache-flex/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Apache flex Flex:将事件分配给动态创建的按钮_Apache Flex_Actionscript_Text_Button_Text Files - Fatal编程技术网

Apache flex Flex:将事件分配给动态创建的按钮

Apache flex Flex:将事件分配给动态创建的按钮,apache-flex,actionscript,text,button,text-files,Apache Flex,Actionscript,Text,Button,Text Files,我的应用程序有一些按钮,用户可以按这些按钮将预定义的字符串插入文本区域。我现在正在动态加载按钮值,以便用户可以定义自己的按钮 我正在使用一个buttons.txt,它在每一行上包含一个不同的标签(button1、button2、button3等)。我循环浏览文本文件并将按钮添加到组中。这一切都可行,但现在困难的部分。如何将eventlistener分配给这些按钮,以便它们将文本输出到屏幕 protected function view1_viewActivateHandler(event:Vie

我的应用程序有一些按钮,用户可以按这些按钮将预定义的字符串插入文本区域。我现在正在动态加载按钮值,以便用户可以定义自己的按钮

我正在使用一个buttons.txt,它在每一行上包含一个不同的标签(button1、button2、button3等)。我循环浏览文本文件并将按钮添加到组中。这一切都可行,但现在困难的部分。如何将eventlistener分配给这些按钮,以便它们将文本输出到屏幕

protected function view1_viewActivateHandler(event:ViewNavigatorEvent):void
        {
            var path:File = File.documentsDirectory.resolvePath("buttons.txt");
            myTextLoader.load(new URLRequest("file://" +path.nativePath));
            myTextLoader.addEventListener(Event.COMPLETE, onLoaded);
            trace(path.nativePath); // Traces correct file path

            mainTextField.addEventListener(SoftKeyboardEvent.SOFT_KEYBOARD_ACTIVATE, resizeTextField);
            if(data!=null){
                mainTextField.text = data.toString();
            }else{
                mainTextField.text = tags;
            }
        }

        protected function onLoaded(e:Event):void {
            var myArrayOfLines:Array = e.target.data.split(/\n/);
            var tempBtn:Button;
            trace(myArrayOfLines[0]);
            for(var i:Number = 0;i < myArrayOfLines.length;i++){
                tempBtn = new Button();
                tempBtn.label = myArrayOfLines[i];
                btnArray.push(tempBtn);
                group.addElement(btnArray[i]);
            }
        }
受保护的函数view1\u viewActivateHandler(事件:ViewNavigatorEvent):无效
{
变量路径:File=File.documentsDirectory.resolvePath(“buttons.txt”);
加载(新的URLRequest(“文件:/”+path.nativePath));
myTextLoader.addEventListener(Event.COMPLETE,onLoaded);
跟踪(path.nativePath);//跟踪正确的文件路径
mainTextField.addEventListener(SoftKeyboardEvent.SOFT_KEYBOARD_ACTIVATE,resizeTextField);
如果(数据!=null){
mainTextField.text=data.toString();
}否则{
mainTextField.text=标记;
}
}
已加载受保护函数(e:事件):无效{
var myArrayOfLines:Array=e.target.data.split(/\n/);
var tempBtn:按钮;
微量元素(myArrayOfLines[0]);
for(变量i:Number=0;i
编辑

    protected function onLoaded(e:Event):void {
                var myArrayOfLines:Array = e.target.data.split(/\n/);
                var tempBtn:Button;

                for(var i:Number = 0;i < myArrayOfLines.length;i++){
                    var j:Number = i+1;
                    tempBtn = new Button();
                    tempBtn.id = "btn" + i;
                    tempBtn.addEventListener(MouseEvent.CLICK, function(evt:MouseEvent):void{
                        var index:uint = parseInt(evt.currentTarget.id.replace("btn", ""));
                                    //TextArea code will go here

trace(text); // Traces null
                    });
                    tempBtn.label = myArrayOfLines[i];
                    btnArray.push(tempBtn);
                    group.addElement(btnArray[i]);
                }
            }


buttons.txt
     button1_label
    "Hello"
    button2_label
    "Goodbye"
    button3_label
    "Come again"
已加载受保护函数(e:事件):无效{
var myArrayOfLines:Array=e.target.data.split(/\n/);
var tempBtn:按钮;
for(变量i:Number=0;i
不清楚要添加的文本是按钮的同一标签还是不同的文本。无论如何,在创建按钮时,可以添加事件列表器。假设txtArr是一个包含要添加的字符串的简单数组

for(var i:Number = 0;i < myArrayOfLines.length;i++){
    tempBtn = new Button();
    tempBtn.id = "btn" + i;
    tempBtn.addEventListener(MouseEvent.CLICK, function(evt:MouseEvent):void{
        var index = parseInt(evt.currentTarget.id.replace("btn", ""));
        var text:String = textArr[i] as String;
    });
    tempBtn.label = myArrayOfLines[i];
    btnArray.push(tempBtn);
    group.addElement(btnArray[i]);
}
for(变量i:Number=0;i
简单地说,只需使用id字段存储按钮的当前id,然后在触发事件时从id中获取索引号并从数组中读取文本。然后,您只需将文本字符串添加到文本区域


希望这有帮助

哦,对不起,这是不同的文本。例如,Button1作为标签“Hello”作为字符串请查看我的更新代码和txt文件结构的编辑,正如我所说的,只需将要放入textarea的文本放入txtArr数组,就像将按钮标签的文本放入myArayOfLines一样