【问题标题】:How do I access an mxml component through an actionscript variable in adobe flex?如何通过 adobe flex 中的 actionscript 变量访问 mxml 组件?
【发布时间】:2013-01-12 12:36:14
【问题描述】:

假设我有一个按钮

 <s:Button id = "button1" label="Click" click = "buttonHandler()"/>

我还有另一个按钮,应该通过调用以下函数来删除此按钮

 protected function remove_Button(event:MouseEvent):void
 {
      var button1:Button = ?????
      this.removeChild(button1);
 }

我如何首先在mxml文件中将变量声明为同一个按钮?

【问题讨论】:

    标签: actionscript


    【解决方案1】:

    如果按钮和第二个按钮的相应代码在同一个 MXML 文档中,您可以简单地通过分配给它的 ID 引用第一个按钮,在这种情况下为 button1

    Example.mxml:

    <s:Button id = "button1" label="Click" click = "buttonHandler()"/>
    <s:Button label="Remove 1st Button" click = "remove_Button(event)"/>
    
    // this code appears inside a script block in Example.mxml
    protected function remove_Button(event:MouseEvent):void
    {
            // no need to declare button1 as a variable, that has already been
            // done in the 1st <Button> object above
            this.removeChild(button1);
    }
    

    应该注意,您使用 MXML 标记声明的任何对象都是该 MXML 文档的公共变量。因此,如果第二个按钮位于不同的 MXML 文档/类中,那么您仍然可以通过您分配的 ID 访问它(即:button1)。

    【讨论】:

      【解决方案2】:

      如果您的皮肤不支持 this.removeChild 函数,请在 remove_Button 函数中写入 this.removeElement(button1);

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-04-01
        • 1970-01-01
        • 1970-01-01
        • 2011-10-15
        • 2014-01-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多