Flex中某一时间段内使按钮处于无效状态的例子
By Minidxer | July 27, 2008
在注册某个网站账户的时候,通常我们需要阅读相关协议,这个时候很多都采用了某一定时间内“同意”按钮无效的机制。接下来的例子就演示了这一做法的Flex实现。
让我们先来看一下Demo(可以右键View Source或点击这里察看源代码):
下面是完整实现代码(或点击这里察看):
Download: main.mxml
- <?xml version="1.0" encoding="utf-8"?>
- <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
- layout="vertical"
- verticalAlign="middle"
- backgroundColor="white"
- creationComplete="init()">
- <mx:Script>
- <![CDATA[
- import flash.events.TimerEvent;
- import flash.utils.Timer;
- private const WAIT_NUM_SECONDS:int = 3;
- private var timer:Timer;
- private function init():void {
- timer = new Timer(1000, WAIT_NUM_SECONDS);
- timer.addEventListener(TimerEvent.TIMER, onTimer);
- timer.start();
- okButton.enabled = false;
- okButton.label = okButton.data + " (" + WAIT_NUM_SECONDS + ")";
- }
- private function onTimer(evt:TimerEvent):void {
- var count:int = timer.repeatCount - timer.currentCount;
- if (count > 0) {
- okButton.label = okButton.data + " (" + count + ")";
- } else {
- okButton.enabled = true;
- okButton.label = okButton.data.toString();
- }
- }
- ]]>
- </mx:Script>
- <mx:ApplicationControlBar dock="true">
- <mx:Button label="Reset button" click="init()" />
- </mx:ApplicationControlBar>
- <mx:Button id="okButton"
- data="OK"
- width="80" />
- </mx:Application>
代码:Peter deHaan 翻译/整理/编译:minidxer