Flex中某一时间段内使按钮处于无效状态的例子

By Minidxer | July 27, 2008

在注册某个网站账户的时候,通常我们需要阅读相关协议,这个时候很多都采用了某一定时间内“同意”按钮无效的机制。接下来的例子就演示了这一做法的Flex实现。

让我们先来看一下Demo可以右键View Source或点击这里察看源代码):


下面是完整实现代码(或点击这里察看):

Download: main.mxml
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
  3.         layout="vertical"
  4.         verticalAlign="middle"
  5.         backgroundColor="white"
  6.         creationComplete="init()">
  7.  
  8.     <mx:Script>
  9.         <![CDATA[
  10.             import flash.events.TimerEvent;
  11.             import flash.utils.Timer;
  12.  
  13.             private const WAIT_NUM_SECONDS:int = 3;
  14.  
  15.             private var timer:Timer;
  16.  
  17.             private function init():void {
  18.                 timer = new Timer(1000, WAIT_NUM_SECONDS);
  19.                 timer.addEventListener(TimerEvent.TIMER, onTimer);
  20.                 timer.start();
  21.  
  22.                 okButton.enabled = false;
  23.                 okButton.label = okButton.data + " (" + WAIT_NUM_SECONDS + ")";
  24.             }
  25.  
  26.             private function onTimer(evt:TimerEvent):void {
  27.                 var count:int = timer.repeatCount - timer.currentCount;
  28.                 if (count > 0) {
  29.                     okButton.label = okButton.data + " (" + count + ")";
  30.                 } else {
  31.                     okButton.enabled = true;
  32.                     okButton.label = okButton.data.toString();
  33.                 }
  34.             }
  35.         ]]>
  36.     </mx:Script>
  37.  
  38.     <mx:ApplicationControlBar dock="true">
  39.         <mx:Button label="Reset button" click="init()" />
  40.     </mx:ApplicationControlBar>
  41.  
  42.     <mx:Button id="okButton"
  43.             data="OK"
  44.             width="80" />
  45.  
  46. </mx:Application>
代码:Peter deHaan 翻译/整理/编译:minidxer

Topics: Flex | Tags: , ,

Related Post

Leave a Comment

Name(*):

E-Mail(*) :

Website :

Comments :

Search Posts

Archives

Sponsored Ads