2012-11-07 23 views
0

我可以找到MX组件的东西,但是我不能在Spark组件上工作。在火花标签上显示当前日期移动应用程序Flex Builder

我只想在标签中显示当前日期和日期。

我曾经尝试这样做(见代码),但我只拿回功能函数(){}

<?xml version="1.0" encoding="utf-8"?> 
    <s:View xmlns:fx="http://ns.adobe.com/mxml/2009" 
      xmlns:s="library://ns.adobe.com/flex/spark" 
      currentState="login" tabBarVisible="{currentState!='login'}"> 

     <s:states> 
      <s:State name="login"/> 
      <s:State name="planner"/> 
     </s:states> 

<fx:Script> 
    <![CDATA[ 

     import mx.rpc.events.ResultEvent; 
//private function login() :void 
//{ 
//loginservice.send(); 
//} 

     private function checkLogin(evt:ResultEvent):void 

     { 

      if(loginservice.lastResult.loginsuccess == "yes") 

      { 

       currentState = "planner"; 

      } 
      if(evt.result.loginsuccess == "no") 

      { 
       statusLabel.text = "Incorrect. (Try user/password)"; 

      }  

     } 

     protected function submit_clickHandler(event:MouseEvent):void 
     { 
      // TODO Auto-generated method stub 

     } 



     public function displayDate():void { 
      var now:Date = new Date(); 
      var day_int:int = now.getDate(); //gets day of the month 
      var month_int:int = (now.getMonth()+1); // gets month. Months are given from 0 to 11, so you need to do +1 here 
      var year_int:int = now.getFullYear(); // gets year 

      var day_string:String; 
      //display always 2 digits for a month, so '02' instead of just '2' for February 
      if (day_int < 10) { 
       day_string = "0" + day_int.toString(); 
      } 
      else { 
       day_string = day_int.toString(); 
      } 

      var month_string:String; 
      //display always 2 digits for a day, so '02' instead of just '2' 
      if (month_int < 10) { 
       month_string = "0" + month_int.toString(); 
      } 
      else { 
       month_string = month_int.toString(); 
      } 

      var year_string:String = year_int.toString(); 

      // note: this is displaying the date in the DD/MM/YYYY format, this same format is also set for the Datefield below! 
      //DateSelect.text = day_string + "/" + month_string + "/" + year_string; 
     } 

    ]]> 
</fx:Script> 


    <fx:Declarations> 
     <!-- Place non-visual elements (e.g., services, value objects) here --> 

     <s:HTTPService id="loginservice" 
         method="GET" 
         url="http://localhost:8888/MenuPlannerApp/services/login4.php" 
         useProxy="false" 
         result="checkLogin(event)"> 
      <s:request xmlns=""> 

       <email>{email.text}</email> 

       <password>{password.text}</password> 
      </s:request> 
     </s:HTTPService> 

    </fx:Declarations> 
    <s:BorderContainer includeIn="login" width="100%" height="100%" > 
     <s:Label id="statusLabel" /> 
     <s:TextInput id="email" y="192" left="15" right="15" text="email"/> 
     <s:TextInput id="password" y="247" left="15" right="15" text="password" displayAsPassword="true"/> 
     <s:Button id="submit" y="306" right="15" width="100" height="30" 
        label="Login" click="loginservice.send()"/> 
     <s:Button id="LoginCreate" y="306" left="15" width="100" height="30" 
        label="Create"/> 
     <s:Label x="166" y="39" width="113" height="101" fontSize="30" 
       text="Menu&#xd;Planner"/> 
    </s:BorderContainer> 
    <s:BorderContainer includeIn="planner" width="100%" height="100%"> 
    <s:Label text="{displayDate}" /> 


<s:HGroup y="10" width="320" height="46" horizontalAlign="center" 
      textAlign="center"> 

</s:HGroup> 
    <s:VGroup includeIn="planner" > 
    </s:VGroup> 

    </s:BorderContainer> 
</s:View> 

回答

0

改变这样的:

// offset is today +/- a set number of days 
// displayDate(-1) returns string for yesterday 
// displayDate(1) returns string for tomorrow etc 
public function displayDate(offset:int=0):String { 
    var formattedString:String; 

    var now:Date = new Date(); 
    var day_int:int = now.getDate() + offset; //gets day of the month 
    var month_int:int = (now.getMonth()+1); // gets month. Months are given from 0 to 11, so you need to do +1 here 
    var year_int:int = now.getFullYear(); // gets year 

    var day_string:String; 
    //display always 2 digits for a month, so '02' instead of just '2' for February 
    if (day_int < 10) { 
     day_string = "0" + day_int.toString(); 
    } 
    else { 
     day_string = day_int.toString(); 
    } 

    var month_string:String; 
    //display always 2 digits for a day, so '02' instead of just '2' 
    if (month_int < 10) { 
     month_string = "0" + month_int.toString(); 
    } 
    else { 
     month_string = month_int.toString(); 
    } 

    var year_string:String = year_int.toString(); 

    formattedString = day_string + "/" + month_string + "/" + year_string; 

    return formattedString; 
} 

的MXML中你可以使用它像这样:

<s:Label text="{ displayDate(-3) }" /> 
+0

这给了我,作为一个小白“你格式化的结果” ......抱歉,但我怎么能得到所显示的一天吗? –

+0

编辑它以包含您的代码 –

+0

谢谢! 还有两个问题。我想要显示当前日期,前3天以及即将到来的3天。所以基本上是整整一周的日期,每天它会移动到第二天。 我知道我可以做+,然后在第二天加1,但我怎么能做到这一点,而不必复制7次显示剂量。 –