私のアプリには、ユーザがテキストボックスに定義済みの文字列を挿入するためのボタンがあります。私は今、ボタンの値を動的にロードして、ユーザーが独自のボタンを定義できるようにしています。Flex:動的に作成されたボタンにイベントを割り当てる
私は、各行に異なるラベル(button1、button2、button3など)を含むbuttons.txtを使用しています。私はテキストファイルをループし、ボタンをグループに追加します。これはすべて機能しますが、今は難しい部分です。これらのボタンにイベントリスナーを割り当てて、テキストを画面に出力するにはどうすればよいですか?
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]);
}
}
EDIT
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"
に役立ちます! :) – RapsFan1981
ああ、申し訳ありませんそれは別のテキストです。例えば、Button1をラベル "Hello"として文字列 – RapsFan1981
私のtxtファイル – RapsFan1981