的Flex / ActionScript代码:如何更改所有按钮的属性(标签)? [英] Flex/Actionscript : How to change property (label) of all buttons?

查看:621
本文介绍了的Flex / ActionScript代码:如何更改所有按钮的属性(标签)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的Flex / ActionScript和有一个非常基本的问题。

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

比方说,我有大约100 +按钮。它们都具有相同的标签。现在我想改变标签都带着点击。我可以给他们的ID,然后运行一个循环,但我在寻找一个整洁的解决方案。

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.

感谢您的帮助。

嗯,这是行不通的,因为按钮标签在运行时被修改。 这是code

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.

推荐答案

关闭我的头顶,我可以为您提供几种解决方案,根据您的整洁的定义。我猜最巧妙将被调度的事件。不过,我将概述三个:

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标签本身是一个事件调度程序或某些对象,可以通知任何被标记为有其特定的价值。这可以被看作是溶液#当然3的变型,但并不。你的按钮将注册他们被分配在标签上的听众,并通过标签更改操作调度change事件的按钮将有机会更新他们的标签显示。这是整齐的,因为你可以有一个可重复使用的通用的编程模式以及:

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之一,像你想到了什么,但觉得还不够整齐,我以为会是更快的更新是保持轨道的按钮具有相同的标签在容器中,很容易可重复的,例如:字典(需弱密钥,这样一个载体不会做!)

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.使用显示列表层次结构和事件流模型有Flash Player的利益做的大部分工作对您:

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 :-)

这篇关于的Flex / ActionScript代码:如何更改所有按钮的属性(标签)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆