2013-10-30 59 views
0

有一個陣列和textfiled型輸入AS3文本字段中輸入一定數目的字符的

myarray=["h","e","l","l","o"]; 

我需要僅一個「H」,「E」,「O」和兩次「l」的按鍵輸入到文本框

這是可能嗎?

+0

你想驗證的東西嗎? – Zeus

+0

我不知道你在說什麼 – Xiler

+0

是的,但是直到輸入數組元素和字符 例子1次「h」,1次「e」,2次「l」,1次「o」, 不是別的輸入人物 – ncinar

回答

0

我試過並找到了。謝謝你的另一個答案

import flash.text.TextField; 

var tf:TextField = new TextField ; 
tf.border = true; 
tf.type = "input"; 
addChild(tf); 
tf.addEventListener(KeyboardEvent.KEY_DOWN,reportKeyDown); 
var dizi:Array = "hello".split(""); 
var yazilan:Array=new Array(); 
function reportKeyDown(event:KeyboardEvent):void 
    { 
     tf.restrict = dizi.toString(); 
     var harfsira:int; 
     harfsira = dizi.indexOf(String.fromCharCode(event.charCode)); 
     trace(harfsira); 
     trace("character: " + String.fromCharCode(event.charCode) +   " (key code: " + event.keyCode + " character code: "   + event.charCode + ")"); 
      if (harfsira >-1) 
       { 
        yazilan.push(String.fromCharCode(event.charCode)); 
        dizi.splice((harfsira),1); 
        trace(yazilan.toString()+" "+"input character"); 
        trace(dizi.toString()+" "+"restrict characters"); 
       } 
       else 
       { 
        trace("end of restrict character"); 
       } 
    } 
0

這裏是不關心順序的解決方案:

public class Main extends Sprite 
{ 
    var myarray:Array = ["h","e","l","l","o"]; 

    public function Main():void 
    { 
     if (stage) init(); 
     else addEventListener(Event.ADDED_TO_STAGE, init); 
    } 

    private function init(e:Event = null):void 
    { 
     removeEventListener(Event.ADDED_TO_STAGE, init); 

     var tf:TextField = new TextField(); 
     tf.type = TextFieldType.INPUT; 
     tf.border = true; 
     tf.addEventListener(TextEvent.TEXT_INPUT, onInput); 
     addChild(tf); 

    } 

    private function onInput(e:TextEvent):void 
    { 
     var index:int = myarray.indexOf(e.text); 
     if (index == -1) { 
      e.preventDefault(); 
     } else { 
      myarray.splice(index, 1); 
     } 
    } 

} 

如果你想輸入的完全匹配的數組中的順序,那麼你需要檢查,如果在陣列比賽的第一個字符輸入,然後從陣列中刪除:

private function onInput(e:TextEvent):void 
    { 
     if (!myarray.length || myarray[0] != e.text) { 
      e.preventDefault(); 
     } else { 
      myarray.splice(0, 1); 
     } 
    } 
0

雖然你的問題是相當神祕,我會嘗試儘可能地回答。

TextField有一個限制允許輸入的內置restrict屬性。

這意味着您可以執行類似textField.restrict = "helo";的操作,並且只允許用戶輸入字母h,e,l和o。

這並沒有解決這個問題,它似乎只是想讓用戶只有輸入單詞'hello'。對於這一點,你必須捕捉用戶輸入,因爲它進來,防止任何不必要的字符:

import flash.text.TextField; 
import flash.text.TextFieldType; 
import flash.events.TextEvent; 

var restrictionArray:Array = "hello".split(""); 
var currentLetterIndex:int = 0; 
var restrictedTextField:TextField = new TextField(); 
addChild(restrictedTextField); 
restrictedTextField.restrict = "helo"; 

restrictedTextField.type = TextFieldType.INPUT 
restrictedTextField.width = 100; 
restrictedTextField.height = 30; 
restrictedTextField.addEventListener(TextEvent.TEXT_INPUT, onKey); 
function onKey(e:TextEvent):void { 
    if(currentLetterIndex < restrictionArray.length && e.text == restrictionArray[currentLetterIndex]) 
    { 
     currentLetterIndex++; 
    } 
    else 
    { 
     e.preventDefault(); 
    } 
} 

上面的代碼跟蹤哪些字母應該由restrictionArraycurrentLetterIndex的方式來下。通過使用preventDefault方法,任何不匹配的字母都不會輸入到TextField中。