flex - 将按钮文本更改为随机xml条目onclick

时间:2011-12-30 06:39:19

标签: xml flex random flash-builder

我有一个我正在使用flex的应用程序,当按下单击按钮时,我想将按钮的文本更改为xml中的随机条目.... xml位于assets文件夹中并标题为games.xml。我希望在按下按钮时选择一个随机游戏。

这里是games.xml

<games>
    <game> GameName1
    <description> description1 </description>
    </game>
    <game> GameName2
    <description> description2 </description>
    </game>
    <game> GameName3
    <description> description3 </description>
    </game>
</games>

这是flex文件

<?xml version="1.0" encoding="utf-8"?>
<s:View xmlns:fx="http://ns.adobe.com/mxml/2009"
    xmlns:s="library://ns.adobe.com/flex/spark"
    backgroundColor="#0000FF" title="games!">

<fx:Script>
    <![CDATA[

        protected function button1_clickHandler(event:MouseEvent):void
        {
            // TODO Auto-generated method stub
            gamebutton.label="test"   <---i want this to be a random game name
        }
    ]]>
</fx:Script>

<fx:Declarations>
    <!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<s:Button id="gamebutton" click="button1_clickHandler(event)" horizontalCenter="0" top="10" x="0" width="95%" label="Pick A Game"/>

</s:View>

感谢您提供的任何帮助!

1 个答案:

答案 0 :(得分:1)

您只需将以下代码放入button1_clickHandler

即可
protected function button1_clickHandler(event:MouseEvent):void
{
    var num:int = (Math.random() * (4 - 1)) + 1
    // TODO Auto-generated method stub
    gamebutton.label="GameName" + num.toString();
}

这里4是最大游戏+ 1,1是munimum.and num是1到3之间的随机数。

度过愉快的一天。