簡體   English   中英

Flex - 如何在 Flash Builder 中進行計算

[英]Flex - How to do calculation in Flash 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" title="BMI Calculator">

<fx:Script>
    <![CDATA[
        protected function calculate_clickHandler(event:MouseEvent):void
        {
            // TODO Auto-generated method stub

        }
    ]]>
</fx:Script>

<fx:Declarations>
    <!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<s:actionContent>
    <s:Button label="Back" click="navigator.pushView(MainHomeView)" styleName="back"/>
</s:actionContent>
<s:Label x="33" y="61" fontSize="30" text="Weight(kg) :"/>
<s:Label x="34" y="140" fontSize="30" text="Height(cm) :"/>
<s:TextInput id="mywieght" x="216" y="40" width="228" prompt="0.0kg" textAlign="right"/>
<s:TextInput id="myheight" x="216" y="119" width="228" prompt="0.0cm" textAlign="right"/>
<s:Button id="calculation" x="31" y="260" width="413" label="Calculate" fontSize="36"
          fontStyle="italic"/>
<s:Label id="myresult" left="31" right="36" height="146" fontSize="72" fontStyle="normal"
         fontWeight="bold" text="0.0" textAlign="center" verticalAlign="middle"
         verticalCenter="99"/>
</s:View>

這是 BMI 計算器的 gui。 我真的沒有使用 Flash Builder 的基本知識。 誰能教我如何使用用戶在 textinput 中輸入的數據,然后將其用於計算和顯示? 謝謝

首先,您為按鈕分配一個單擊處理程序,以便在單擊按鈕時調用您的 calculate_clickHandler 方法:

<s:Button id="calculation" x="31" y="260" width="413" label="Calculate" fontSize="36" fontStyle="italic" click="calculate_clickHandler(event)"/>

然后進行計算並將結果傳遞給 myresult 標簽:

protected function calculate_clickHandler(event:MouseEvent):void
{
    var height:Number = Number(myheight.text);
    var weight:Number = Number(mywieght.text); // you have a typo here, wiegth instead of weight ;)

    myresult.text = height * weight;
}

並且您可能希望在單擊后退按鈕時使用 popView()。 這將刪除當前視圖並返回到上一個視圖。 push() 將添加另一個視圖:

<s:Button label="Back" click="navigator.popView()" styleName="back"/>

嘗試這個:

<?xml version="1.0"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark">
<fx:Script>
<![CDATA[
    protected function calculate_clickHandler(event:MouseEvent):void
    {
        // TODO Auto-generated method stub
        const myWeightInKG:Number = Number(myWeight.text);
        const myHeightInMeter:Number = Number(myHeight.text) * 0.01;
        const BMI:Number = myWeightInKG / (myHeightInMeter * myHeightInMeter);
        const BMI_withOneDecimalPlace:Number = int(BMI * 10)/10;
        myResult.text = BMI_withOneDecimalPlace.toString();
    }
    ]]>
</fx:Script>

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

<s:Label x="33" y="61" fontSize="30" text="Weight(kg) :"/>
<s:Label x="34" y="140" fontSize="30" text="Height(cm) :"/>
<s:TextInput id="myWeight" x="216" y="40" width="228" prompt="Weight in Kg(e.g. 56)" textAlign="right"/>
<s:TextInput id="myHeight" x="216" y="119" width="228" prompt="Weight in cm(e.g. 157)" textAlign="right"/>
<s:Button id="calculation" x="31" y="260" width="413" label="Calculate" fontSize="36"
          fontStyle="italic" click="calculate_clickHandler(event)"/>
<s:Label id="myResult" left="31" right="36" height="146" fontSize="72" fontStyle="normal"
         fontWeight="bold" text="0.0" textAlign="center" verticalAlign="middle"
         verticalCenter="99"/>
</s:Application>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM