代码之家  ›  专栏  ›  技术社区  ›  Treby

Flex:只接受数字的文本输入

  •  24
  • Treby  · 技术社区  · 15 年前

    需要一个只接受数字的代码。输入时,代码必须检查是否为数字,如果不是,则必须删除输入的键或根本不输入

    8 回复  |  直到 15 年前
        1
  •  31
  •   Gregor Kiddie    15 年前

    查看TextInput类上的restrict属性。将其设置为“0-9”

        2
  •  13
  •   Sri    15 年前
       <s:TextInput id="textInput"
                    restrict="0-9"
                    widthInChars="20"
                    maxChars="20" />
       <mx:TextInput id="textInput"
                    restrict="0-9"
                    widthInChars="20"
                    maxChars="20" />
    
        3
  •  3
  •   sly1024    13 年前

    有一个叫做NumericStepper的控件。

    见: http://livedocs.adobe.com/flex/3/html/help.html?content=controls_11.html

    干杯 狡猾的

        4
  •  2
  •   Shawn Chin    13 年前
    <?xml version="1.0"?>
    <!-- Simple example to demonstrate the TextInput control. -->
    <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" viewSourceURL="srcview/index.html">
    
        <mx:Panel title="Dodawanie dwóch liczb :)" height="279" width="238" 
            paddingTop="10" paddingLeft="10">
    
            <mx:TextInput id="src"
              restrict="0-9"
                    maxChars="20" />
            <mx:TextInput id="dest"
              restrict="0-9"
                    maxChars="20"/>
    
            <mx:Button label="dodaj" click= "dodaj();" id="but"/>
            <mx:Label text="Suma" width="59"/>
            <mx:Label text="0" width="160" id="wynik"/>
    
        </mx:Panel>
        <mx:Script>
         <![CDATA[
          import mx.formatters.NumberBase;
          public function dodaj():Number
          {
           var liczba:Number = Number(src.text) + Number(dest.text);
           wynik.text = liczba.toString();
           return 0;
          }
    
         ]]>
        </mx:Script>
    </mx:Application>
    
        5
  •  1
  •   Mihai Nita    15 年前
        6
  •  1
  •   Rahul Singhai    11 年前

    我用的是

    <s:TextInput id="textInput"
        restrict="0-9.\\-"
        change="onChangeNumberTextInput(event, 6)"/>
    
    private function onChangeNumberTextInput(event:TextOperationEvent, precision:uint = 2):void
        {
            var strNumber:String = "";
            if (event.currentTarget is mx.controls.TextInput)
                strNumber = (event.currentTarget as mx.controls.TextInput).text;
            else if (event.currentTarget is spark.components.TextInput)
                strNumber = (event.currentTarget as spark.components.TextInput).text;
            else
                return;
    
            var ind:int = strNumber.indexOf(".");
            if (ind > -1)
            {
                var decimal:String = strNumber.substring(ind + 1);
                if (decimal.indexOf(".") > -1)
                    strNumber = strNumber.substring(0, ind + 1 + decimal.indexOf("."));
                if (decimal.length > precision)
                    strNumber = strNumber.substring(0, ind + 1 + precision);
            }
    
            if (event.currentTarget is mx.controls.TextInput)
                (event.currentTarget as mx.controls.TextInput).text = strNumber;
            else if (event.currentTarget is spark.components.TextInput)
                (event.currentTarget as spark.components.TextInput).text = strNumber;
        }
    

    change listener函数删除小数点或第二次出现的“.”以外的所有字符:

        7
  •  0
  •   Ryan Watts    11 年前

    您需要更改属性,以便应用程序仅从应用程序请求数字键盘。

    试试“软键盘”数字

        8
  •  0
  •   ketan    9 年前

    我不知道你到底想做什么。如果你只想把这两个加起来,用下面的方法

    {parseInt(txt1.text) + parseInt(txt2.text)}
    

    推荐文章