2009-11-17 35 views
23

Cần mã chỉ chấp nhận số. Khi nhập, mã phải kiểm tra xem đó có phải là số không, nếu không, mã phải xóa khóa đã nhập hoặc không nhập mã đó vào tất cảFlex: Nhập văn bản chỉ chấp nhận số

+0

Xem thêm: http: //stackoverflow.com/questions/6300528/flex-restrict-textinput-to-accept-only-decimal-numbers –

Trả lời

30

xem thuộc tính giới hạn trên lớp TextInput. Đặt nó là "0-9"

+0

về dấu thập phân. tôi có thể bao gồm điều đó không? – Treby

+0

Có, chỉ là ".0-9" nếu tôi xếp lại chính xác. Lưu ý rằng họ sẽ có thể thêm nhiều hơn một. nếu bạn làm theo cách này. Nếu bạn đang hạn chế họ tạo một số hợp pháp, bạn sẽ cần thêm một số AS để xử lý nó. –

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

Tôi không chắc chắn chính xác những gì bạn muốn làm. Nếu bạn chỉ muốn tính tổng hai, sử dụng sau

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

ví dụ của bạn chỉ cần ghép nối hai chuỗi đó. Ví dụ này cố gắng chuyển đổi văn bản thành số và sau đó tổng hợp hai giá trị đó.

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

Bạn cần phải thay đổi thuộc tính sao cho các ứng dụng chỉ yêu cầu bàn phím số từ ứng dụng.

thử số "SoftKeyboard" "; '

1

tôi sử dụng somthing như

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

Chức năng thay đổi người nghe loại bỏ tất cả mọi thứ vượt quá số ký tự chính xác từ dấu thập phân, hoặc bất kỳ lần xuất hiện thứ hai của '':

Các vấn đề liên quan