当LinkButton无效时,光标不显示为手型
在Flex组件LinkButton里,我们可以用useHandCursor属性来控制是否使用手型光标。现在我们要实现在LinkButton的enable=false时,useHandCursor=false。反过来,他们要同时为true。
下面的例子显示了当Flex里的LinkButton不可用时,如何移除它上的手型光标,让光标回到箭头。
我们采用的办法是扩展LinkButton类,覆盖了enabled的setter方法,在里面加上了改变useHandCursor属性的语句。
请看下面的例子:
main.mxml
<?xml version="1.0" encoding="utf-8"?> <!-- http://blog.flexexamples.com/2008/09/07/removing-the-hand-cursor-from-a-disabled-linkbutton-control-in-flex/ --> <mx:Application name="LinkButton_useHandCursor_test" xmlns:mx="http://www.adobe.com/2006/mxml" xmlns:comps="comps.*" layout="horizontal" verticalAlign="middle" backgroundColor="white"> <mx:ApplicationControlBar dock="true"> <mx:Form styleName="plain"> <mx:FormItem label="enabled:"> <mx:CheckBox id="checkBox" selected="true" /> </mx:FormItem> </mx:Form> </mx:ApplicationControlBar> <mx:LinkButton id="linkButton" label="Default LinkButton" enabled="{checkBox.selected}" /> <comps:DisabledLinkButtonMXML id="linkButton2" label="Custom LinkButton (MXML)" enabled="{checkBox.selected}" /> <comps:DisabledLinkButtonAS id="linkButton3" label="Custom LinkButton (ActionScript)" enabled="{checkBox.selected}" /> </mx:Application>
DisabledLinkButtonMXML组件:
<?xml version="1.0" encoding="utf-8"?> <!-- http://blog.flexexamples.com/2008/09/07/removing-the-hand-cursor-from-a-disabled-linkbutton-control-in-flex/ --> <mx:LinkButton xmlns:mx="http://www.adobe.com/2006/mxml"> <mx:Script> <![CDATA[ override public function set enabled(value:Boolean):void { //让enabled和useHandCursor同步 super.enabled = value; useHandCursor = value; } ]]> </mx:Script> </mx:LinkButton>
DisabledLinkButtonAS组件
/** * http://blog.flexexamples.com/2008/09/07/removing-the-hand-cursor-from-a-disabled-linkbutton-control-in-flex/ */ package comps { import mx.controls.LinkButton; public class DisabledLinkButtonAS extends LinkButton { /** * Constructor. */ public function DisabledLinkButtonAS() { //调用父类的构造函数 super(); } /** * @private */ override public function set enabled(value:Boolean):void { super.enabled = value; useHandCursor = value; } } }
很显然, DisabledLinkButtonMXML和 DisabledLinkButtonAS组件实现的是一样的功能。只是用的语法不一样。