-1

2 つのケースでテキスト入力を使用して (+、-、​​/、*) のような数学計算を正しく実装する方法を教えてもらえますか?

1 - より多くの状態
2 - ビューに入力し、結果を他のビューに表示する

ここに私のコードの一部があります:

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

1 に答える 1

0

いくつかの提案がありますが、問題が何であるか正確にはわかりません。

まず、TextAreaでrestrict属性を使用します。restrict 属性は、ユーザーが数字以外のキーを入力できないようにします。このような並べ替え:

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

softKeyboardType をサポートするデバイスでモバイル アプリケーションを使用している場合、softKeyboardType を使用することでこれが問題にならない可能性は十分にあります。

コメントで言及したコード行は、エラーが発生しやすいようです。

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

まず、datot と disp はラベルです。TextInput ではありません。数値以外の値がハードコードされています。datot_val と disp_val を使いたいと思います。これは TextInputs です。それらはすでに入力であるため、そのようにキャストする必要はありません。

次に、TextInput を NumberFormatter としてキャストする必要はありません。通常、NumberFormatter を静的クラスとして使用するのではなく、そのインスタンスを作成します。NumberFormatterの詳細については、こちらを参照してください。

したがって、あなたの行は次のように書き直されると思います。

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

括弧が混乱しないように、いくつかの行を追加しました。datot (別名 datot.text) の入力を取り、それを int としてキャストします。disp (disp.text) の入力を受け取り、それを int としてキャストします。この 2 つを足し合わせて、両方を文字列に変換します。文字列は Primul_val のテキスト値に格納されます。

それは何かを片付けるのに役立ちますか?

于 2012-06-12T14:53:19.147 に答える