Search code examples
apache-flexmathflash-buildersum

How can I do simple text input math calculations in flash-builder?


Can someone show me how should I correctly implement mathematical calculations like (+,-,/,*) using text inputs in 2 cases:

1 - more states
2 - input in a view an the result in other

Here is a part of my code:

<?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"
        creationComplete="init()" currentState="State1" overlayControls="false" title="Calculator">
    <s:states>
        <s:State name="State1"/>
        <s:State name="RezultatCalculator"/>
    </s:states>
    <fx:Declarations>
        <!-- Place non-visual elements (e.g., services, value objects) here -->
    </fx:Declarations>
    <fx:Script>
        <![CDATA[
            import spark.events.DropDownEvent;
            protected function init():void
            {
                addEventListener(TransformGestureEvent.GESTURE_SWIPE, swipeHandler);
            }
            protected function swipeHandler(event:TransformGestureEvent):void
            {
                if (event.offsetX == 1)
                {
                    navigator.pushView(CalculatorView);
                }
            }




            protected function primulcalcul_openHandler(event:DropDownEvent):void
            {
                // TODO Auto-generated method stub

            }

        ]]>
    </fx:Script>
    <s:Scroller includeIn="State1" left="3" right="3" top="0" height="547">
        <s:VGroup width="100%" height="100%" gap="24" horizontalAlign="justify" paddingTop="8"
                  verticalAlign="top">
            <s:Label id="actot" width="237" height="60" text="Active Totale" textAlign="center"
                     verticalAlign="middle"/>
            <s:TextInput id="actot_val" width="183" height="60" fontFamily="_sans" fontSize="28"
                          textAlign="center" softKeyboardType="number" />
            <s:Label id="disp" width="159" height="60" text="Disponibilitati" textAlign="center"
                     verticalAlign="middle"/>
            <s:TextInput id="disp_val" width="164" height="60" fontFamily="_sans" fontSize="28"
                         textAlign="center" softKeyboardType="number"/>
            <s:Label id="datot" width="159" height="60" text="Datorii Totale" textAlign="center"
                     verticalAlign="middle"/>
            <s:TextInput id="datot_val" width="164" height="60" fontFamily="_sans"
                         fontSize="28" textAlign="center" softKeyboardType="number"/>
            <s:Label id="caprop" width="159" height="60" fontSize="24" text="Capitaluri Proprii"
                     textAlign="center" verticalAlign="middle"/>
            <s:TextInput id="caprop_val" width="164" height="60" fontFamily="_sans" fontSize="28"
                         textAlign="center" softKeyboardType="number"/>

            <s:Button id="butstart0" width="401" height="70" label="START"
                      click="currentState='RezultatCalculator'" enabled="true"/>
        </s:VGroup>
    </s:Scroller>
    <s:CalloutButton id="primulcalcul" includeIn="RezultatCalculator" x="22" y="28" width="145"
                     height="63" label="primulcalcul" enabled="true"
                     open="primulcalcul_openHandler(event)"/>
    <s:TextArea id="Primul_val" includeIn="RezultatCalculator" x="203" y="27" width="267"
                editable="false" prompt="result"/>
    <fx:Script>
        <![CDATA[
            import flash.globalization.NumberFormatter;
            import flashx.textLayout.formats.Float;
            import flashx.textLayout.formats.Float;
            import learnmath.mathml.components.MathMLFormula;
            import learnmath.mathml.formula.layout.MathBox;
            public function MathMLFormula():void
            {
                var Primul_val:Number=new Number
                var datot:Number=new Number
                var disp:Number=new Number
                    Primul_val=0
                NumberFormatter(TextArea(Primul_val))==NumberFormatter(TextInput(datot))+NumberFormatter(TextInput(disp)); /* this is one of the examples, i tied some different values like valueOf */
            }
        ]]>
    </fx:Script>

</s:View>

Solution

  • I have a few suggestions, but I'm not sure exactly what your problem is.

    First, use the restrict attribute on your TextArea. The restrict attribute will prevent people from entering non-numeric keys. Sort of like this:

     <s:TextInput id="datot_val" width="164" height="60" fontFamily="_sans"
                             fontSize="28" textAlign="center" softKeyboardType="number" restrict="0-9"/>
    

    It's entirely possible that the use of the softKeyboardType makes this a non-issue if you're using a mobile application on a device that supports it.

    The line of code you mentioned in the comments looks to be error prone:

    NumberFormatter(TextArea(Primul_val))==NumberFormatter(TextInput(datot))+NumberFormatter(TextInput(disp));
    

    First, datot and disp are Labels; not TextInputs. They have hard coded non-numeric values. I think you want to use datot_val and disp_val. which are TextInputs. Since they are already inputs you do not need to cast them as such.

    Second you do not need to cast the TextInput as a NumberFormatter. You would not usually use a NumberFormatter as a static class, but rather create an instance of it. More info on NumberFormatter here.

    So, I believe your line would be rewritten like this:

           Primul_val.text== String(
                                    int(datot.text) + int(disp.text)
                                   ); 
    

    I added some extra lines so the parenthesis wouldn't get confused. I take the input of datot (AKA datot.text) and cast it as an int. I take the input of disp (disp.text) and cast that as an int. I add the two together and convert them both to a String. The string is stored in the text value of Primul_val.

    Does that help clear anything up?