Flex/Actionscript:如何更改所有按钮的属性(标签)?

发布于 2024-09-06 19:57:04 字数 421 浏览 0 评论 0原文

我是 Flex/Actionscript 新手,有一个非常基本的问题。

假设我有大约 100 多个按钮。它们都有相同的标签。现在我想通过单击更改所有标签。我可以给他们 ID,然后运行 ​​for 循环,但我正在寻找更简洁的解决方案。

感谢您的帮助。

更新:

嗯,它不起作用,因为按钮的标签在运行时被修改。 这是代码

public function changeButton(event:Event):void {
    if (event.target.label != 'X') {
    event.target.label ='X';
    } else {
    event.target.label ='';
    }
}

现在我需要一个按钮,单击该按钮即可删除/用空标签替换该标签。

I'm new to Flex/Actionscript and have a very basic question.

Let's say I have about 100+ buttons. They all have the same label. Now I want to change the label for all with a click. I could give them IDs and then run a for loop, but I'm searching for a neater solution.

Thanks for help.

Update:

Hm, it doesn't work because label of buttons are modified during the run time.
This is the code

public function changeButton(event:Event):void {
    if (event.target.label != 'X') {
    event.target.label ='X';
    } else {
    event.target.label ='';
    }
}

Now I need a button that will on click delete/replace this label with an empty one.

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

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

发布评论

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

评论(2

梦开始←不甜 2024-09-13 19:57:04

如果您使用 Flex,则可以使用数据绑定:

[Bindable] myButtonLabel:String = "Default Label";

<mx:Label id="label1" text="{myButtonLabel}"/>
<mx:Label id="label2" text="{myButtonLabel}"/>

function buttonClick(evt:Event):void {
    myButtonLabel = "New Label";
}

每当您更改 myButtonLabel 的值时,所有标签都会更改。

If you're using Flex, you can use databinding:

[Bindable] myButtonLabel:String = "Default Label";

<mx:Label id="label1" text="{myButtonLabel}"/>
<mx:Label id="label2" text="{myButtonLabel}"/>

function buttonClick(evt:Event):void {
    myButtonLabel = "New Label";
}

Whenever you change the value of myButtonLabel all labels will change.

瑾夏年华 2024-09-13 19:57:04

我可以立即为您提供几种解决方案,具体取决于您对“整洁”的定义。我想“最巧妙的”就是调度一个事件。但我将概述这三个内容:

1. 让标签本身成为一个事件调度程序或某个对象,可以通知任何“被标记”的内容及其特定值。当然,这可以看作是解决方案 #3 的变体,但事实并非如此。您的按钮将在为其分配的标签上注册侦听器,并且通过使标签更改操作分派更改事件,您的按钮将有机会更新其标签显示。这很巧妙,因为您可以沿用一个可重用的通用编程模式:

class Button
{
    var label_tf: TextField;

    public function Button(label: *)
    {
        if(label is IEventDispatcher)
        {
            label.addEventListener("change", on_label_change_event, false, 0, true);
        }

        label_tf.text = label;
    }

    function on_label_change_event(event: Event)
    {
        label_tf.text = event.target;
    }
}

dynamic class DynamicEventDispatcher extends EventDispatcher
{
}

var label = new DynamicEventDispatcher();
label.value = "Cancel"
label.toString = function() { return this.value; };

var button1 = new Button(label);
var button2 = new Button(label);
var buttonN = new Button(label);

/// Now make all buttons display 'Cancel' in Spanish ;-)
label.value = "Anular";
label.dispatchEvent(new Event("change"));

2.另一个,就像您想到的,但认为不够简洁,我认为更新速度会更快,即跟踪具有相同标签的按钮一个易于迭代的容器,例如字典(需要弱键,因此向量不行!)

class Button
{
    static var buttons_by_label: Dictionary;

    static function init_class()
    {
        buttons_by_label = new Dictionary(true);
    }

    static function reset_identical_labels(old_value: String, new_value: String)
    {
        if(buttons_by_label[old_value] === undefined)
        {
            throw new Error("No buttons with specified label value.");
        }

        for(var button: Button in buttons_by_label[old_value])
        {
            button.label = new_value;
        }
    }

    var label_tf: TextField;

    public function Button(label: String)
    {
        this.label = label;
    }

    public function get label()
    {
        return label_tf.text;
    }

    public function set label(new_value: String)
    {
        var old_value: String = label_tf.text;

        if(buttons_by_label[old_value] !== undefined)
        {
            delete buttons_by_label[old_value][this];
        }

        if(buttons_by_label[new_value] === undefined)
        {
            buttons_by_label[new_value] = new Dictionary(true);
        }

        buttons_by_label[new_value][this] = true;

        label_tf.text = new_value;
    }
}

Button.init_class();

var button1 = new Button("Cancel");
var button2 = new Button("Cancel");
var buttonN = new Button("Cancel");

Button.reset_identical_labels("Cancel", "Anular");

3.利用显示列表层次结构和事件流模型的优势,让 Flash Player 为您完成大部分工作:

class Button
{        
    var label_tf: TextField;

    public function Button(label: String)
    {
        this.label = label;

        stage.addEventListener("request.button.update_label", 
            on_update_label_request_event, 
            false, 0,true);
    }

    public function get label()
    {
        return label_tf.text;
    }

    public function set label(new_value: String)
    {            
        label_tf.text = new_value;
    }

    function on_update_label_request_event(event: LabelUpdateRequestEvent)
    {
        if(label == event.old_value)
        {
            label = event.new_value;
        }
    }
}

class LabelUpdateRequestEvent extends Event
{
    public var old_value: String;
    public var new_value: String;

    public function LabelUpdateRequestEvent(old_value: String, new_value: String)
    {
        super("request.button.update_label");

        this.old_value = old_value;
        this.new_value = new_value;
    }
}

var button1 = new Button("Cancel");
var button2 = new Button("Cancel");
var buttonN = new Button("Cancel");

stage.dispatchEvent(new LabelUpdateRequestEvent("Cancel", "Anular"));

我建议您检查每个解决方案的弱点和优点 - 它们都兼具 - 并使用混合解决方案。第一个具有将标签的值与对该值的引用分开的效果,即两个不同的标签对象都以“取消”为值不会彼此改变,即使标签实际上是相同的。其他两个解决方案不会将价值与参考分开。然而,使用它们时,您会因为添加太多事件侦听器和太多按钮而付出额外成本,而第二种解决方案则无法做到这一点。这一切都超出了我的想象,但你有一个有趣的问题,现在就发挥你的幻想:-)

Off the top of my head I can offer you several solutions, depending on your definition of 'neat'. I guess the 'neatest' would be dispatching an event. But I will outline all three:

1.Have label itself be an event dispatcher or some object that can notify whatever "is labelled" with its particular value. This can be seen as a variation of solution #3 of course, but is NOT. Your buttons will register listeners on the label they are assigned, and by making the label change operation dispatch a change event your buttons will have a chance to update their label display. This is neat because you can have a reusable generic programming pattern along:

class Button
{
    var label_tf: TextField;

    public function Button(label: *)
    {
        if(label is IEventDispatcher)
        {
            label.addEventListener("change", on_label_change_event, false, 0, true);
        }

        label_tf.text = label;
    }

    function on_label_change_event(event: Event)
    {
        label_tf.text = event.target;
    }
}

dynamic class DynamicEventDispatcher extends EventDispatcher
{
}

var label = new DynamicEventDispatcher();
label.value = "Cancel"
label.toString = function() { return this.value; };

var button1 = new Button(label);
var button2 = new Button(label);
var buttonN = new Button(label);

/// Now make all buttons display 'Cancel' in Spanish ;-)
label.value = "Anular";
label.dispatchEvent(new Event("change"));

2.Another one, like what you thought of but thought was not neat enough, which I assume would be faster to update is keeping a track of buttons with the same label in a container that is easily iteratable, e.g. a dictionary (need weak keys, so a vector won't do!)

class Button
{
    static var buttons_by_label: Dictionary;

    static function init_class()
    {
        buttons_by_label = new Dictionary(true);
    }

    static function reset_identical_labels(old_value: String, new_value: String)
    {
        if(buttons_by_label[old_value] === undefined)
        {
            throw new Error("No buttons with specified label value.");
        }

        for(var button: Button in buttons_by_label[old_value])
        {
            button.label = new_value;
        }
    }

    var label_tf: TextField;

    public function Button(label: String)
    {
        this.label = label;
    }

    public function get label()
    {
        return label_tf.text;
    }

    public function set label(new_value: String)
    {
        var old_value: String = label_tf.text;

        if(buttons_by_label[old_value] !== undefined)
        {
            delete buttons_by_label[old_value][this];
        }

        if(buttons_by_label[new_value] === undefined)
        {
            buttons_by_label[new_value] = new Dictionary(true);
        }

        buttons_by_label[new_value][this] = true;

        label_tf.text = new_value;
    }
}

Button.init_class();

var button1 = new Button("Cancel");
var button2 = new Button("Cancel");
var buttonN = new Button("Cancel");

Button.reset_identical_labels("Cancel", "Anular");

3.Use the benefits of display list hierarchy and the event flow model to have Flash Player do most of the work for you:

class Button
{        
    var label_tf: TextField;

    public function Button(label: String)
    {
        this.label = label;

        stage.addEventListener("request.button.update_label", 
            on_update_label_request_event, 
            false, 0,true);
    }

    public function get label()
    {
        return label_tf.text;
    }

    public function set label(new_value: String)
    {            
        label_tf.text = new_value;
    }

    function on_update_label_request_event(event: LabelUpdateRequestEvent)
    {
        if(label == event.old_value)
        {
            label = event.new_value;
        }
    }
}

class LabelUpdateRequestEvent extends Event
{
    public var old_value: String;
    public var new_value: String;

    public function LabelUpdateRequestEvent(old_value: String, new_value: String)
    {
        super("request.button.update_label");

        this.old_value = old_value;
        this.new_value = new_value;
    }
}

var button1 = new Button("Cancel");
var button2 = new Button("Cancel");
var buttonN = new Button("Cancel");

stage.dispatchEvent(new LabelUpdateRequestEvent("Cancel", "Anular"));

What I suggest you do is examine the weaknesses and strengths of each - all of them DO have both - and use a hybrid solution. The first has the effect of separating the value of a label with a reference to the value, i.e. two different label objects both with "Cancel" as value will NOT change each other, even though the label is actually identical. The other two solutions don't separate value from reference. With them however you get the extra cost of adding too many event listeners with too many buttons, something the second solution does not do. This all was off the top of my head, but you have an interesting problem, use your fantasy now :-)

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