0

我可以找到 MX 组件的东西,但我不能在 spark 组件上工作。

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

我试过这个(见代码),但我只得到函数 Function(){}

<?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>
4

1 回答 1

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) }" />
于 2012-11-07T18:10:56.633 回答