2009-11-17 16 views
23

È necessario un codice che accetti solo numeri. All'inserimento, il codice deve controllare se è il numero, in caso contrario, deve rimuovere la chiave immessa o non immetterlo affattoFlex: testo che accetta solo numero

+0

Vedi anche: http: //stackoverflow.com/questions/6300528/flex-restrict-textinput-to-accept-only-decimal-numbers –

risposta

30

esaminare la proprietà restrict sulla classe TextInput. Impostarlo su "0-9"

+0

come circa il punto decimale. posso includerlo anche io? – Treby

+0

Sì, è solo ".0-9" se ricalco correttamente. Si noti che saranno in grado di aggiungerne più di uno. se lo fai in questo modo Se le stai limitando a fare un numero legale, avrai bisogno di un AS in più per gestirlo. –

13
<s:TextInput id="textInput" 
       restrict="0-9" 
       widthInChars="20" 
       maxChars="20" /> 
    <mx:TextInput id="textInput" 
       restrict="0-9" 
       widthInChars="20" 
       maxChars="20" /> 
0

Non sono sicuro di cosa esattamente si vuole fare. Se si desidera solo per sommare quei due, utilizzare i seguenti

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

tuo esempio solo concatenare le due stringhe. Questo esempio tenta di convertire il testo in numero e quindi sommare quei due valori.

2
<?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> 
0

è necessario modificare la proprietà in modo che l'applicazione richiesta solo la tastiera il numero dall'applicazione.

provare il numero "SoftKeyboard"; '

1

Io uso somthing come

<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; 
    } 

La funzione di cambiamento ascoltatore rimuove tutto ciò al di là del numero di caratteri di precisione dal punto decimale, o qualsiasi secondo '':

Problemi correlati