为火花皮按钮保持'向下'状态

时间:2010-10-20 18:58:06

标签: flex actionscript flex3 flex4

我在按钮组件中点亮了一个带有向上,向下,向上和禁用状态的按钮以创建模块化。当用户按住某个键时,该按钮是否仍然处于“关闭”状态?

按钮组件包括skinclass:

<?xml version="1.0" encoding="utf-8"?>
<s:Button xmlns:fx="http://ns.adobe.com/mxml/2009" 
    xmlns:s="library://ns.adobe.com/flex/spark" 
    xmlns:mx="library://ns.adobe.com/flex/mx"
    skinClass="wfm.keyBtn">
</s:Button>

火花皮肤:

<?xml version="1.0" encoding="utf-8"?>
<s:SparkSkin xmlns:fx="http://ns.adobe.com/mxml/2009" 
    xmlns:s="library://ns.adobe.com/flex/spark" 
    xmlns:mx="library://ns.adobe.com/flex/mx"
 <fx:Metadata>
  [HostComponent("spark.components.Button")]
 </fx:Metadata>
 <s:states>
  <s:State name="up"/>
  <s:State name="over"/>
  <s:State name="down"/>
  <s:State name="disabled"/>
 </s:states>
 <s:Rect width="18" height="80" height.down="83" radiusX="1">
  <s:stroke>
   <s:SolidColorStroke color="0xaaaaaa" weight="1"/>
  </s:stroke>
  <s:filters>
   <s:BevelFilter
    distance="0"
    angle="5" angle.down="5"
    highlightColor="0xbbbbbb" highlightColor.down="0xaaaaaa"
    highlightAlpha="1" highlightAlpha.down="1"
    shadowColor="0xaaaaaa" shadowColor.down="0xffffff"
    shadowAlpha="1" shadowAlpha.down="1"
    blurX="5" blurX.down="6"
    blurY="5" blurY.down="6"
    strength="2" strength.down="2"
    type="inner"
    />
  </s:filters>
  <s:fill>
   <s:LinearGradient rotation="90">
    <s:GradientEntry color="0xf3f3f3" color.down="0xfff963" />
   </s:LinearGradient>
  </s:fill>
 </s:Rect>
</s:SparkSkin>

1 个答案:

答案 0 :(得分:1)

创建Button的子类,并在您关心的键关闭时告诉皮肤处于关闭状态。此外,每当您检测到键已关闭(或不再关闭)时,使皮肤状态无效,以便皮肤知道检查它应处于什么状态:

package mypackage
{
    import spark.components.Button;

    public class MyButton extends Button
    {
        private var _isKeyDown:Boolean = false;

        private function get isKeyDown():Boolean {
            return _isKeyDown;
        }

        private function set isKeyDown(b:Boolean):void {
            _isKeyDown = b;
            this.invalidateSkinState();
        }

        // Add handlers in here to set isKeyDown to true/false appropriately

        override protected function getCurrentSkinState():String {
            if (isKeyDown)
                return "down";
            else
                return super.getCurrentSkinState();
        }
    }
}