如何将文本分配给自定义HSlider值?

时间:2012-11-03 17:10:52

标签: flash builder

我希望标签显示滑块的自定义值,例如

滑块值等于0,然后标签文本为250 滑块值为1,则标签文本为333 滑块值为2,则标签文本为543 滑块值为3,则标签文本为9342

我是新手,非常感谢任何帮助。我的示例代码是:

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
           xmlns:s="library://ns.adobe.com/flex/spark" 
           xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600">
<fx:Declarations>
    <!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<s:Panel x="199" y="141" width="250" height="200">
    <s:HSlider id="ValueSlider" x="74" y="68" maximum="3" minimum="0" stepSize="1"/>
    <s:Label id="ValueLabel" x="109" y="38" text="Label"/>
</s:Panel>

感谢@Whiteagle这是我现在的,但现在我得到了“无法解决组件实现”错误

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
           xmlns:s="library://ns.adobe.com/flex/spark" 
           xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600">
<fx:Declarations>
    <!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>

<mx:Script>
    <![CDATA[
        import mx.events.SliderEvent;
        protected function ValueSlider_changeHandler(event:SliderEvent):void
    {
        ValueLabel.text = ValueSlider.value;
            if(ValueSlider.value == "0")
            ValueLabel.text = "150";
            if(ValueSlider.value == "1")
            ValueLabel.text = "333";
            if(ValueSlider.value == "2")
            ValueLabel.text = "543";
            if(ValueSlider.value == "3")
            ValueLabel.text = "9342";
    }
    ]]>
</mx:Script>
<s:Panel x="199" y="141" width="250" height="200">
    <s:HSlider id="ValueSlider" x="74" y="68" maximum="5" minimum="0" stepSize="1"/>
    <s:Label id="ValueLabel" x="109" y="38" text="Label"/>
</s:Panel>

1 个答案:

答案 0 :(得分:0)

您只需添加更改事件并更改标签的文本属性:

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
           xmlns:s="library://ns.adobe.com/flex/spark" 
           xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600">
    <fx:Declarations>
        <!-- Place non-visual elements (e.g., services, value objects) here -->
    </fx:Declarations>

    <mx:Script>
    <![CDATA[

        import mx.events.SliderEvent;

        protected function ValueSlider_changeHandler(event:SliderEvent):void
        {
            ValueLabel.text = ValueSlider.value; // You could also use event.currentTarget or similar to obtain the element that dispatched the event, I prefered to refer to it directly for this example.
            // Add your logic here. For instance:
            if(ValueSlider.value == "0") // I'm using an if just for demonstration purposes, if you want to do one check per value you may want to consider using a switch.
                ValueLabel.text = "300";
                ...
        }

    ]]>
    </mx:Script>
    <s:Panel x="199" y="141" width="250" height="200">
        <s:HSlider id="ValueSlider" x="74" y="68" maximum="3" minimum="0" stepSize="1" change="ValueSlider_changeHandler(event)" />
        <s:Label id="ValueLabel" x="109" y="38" text="Label"/>
    </s:Panel>
</s:Application>