0

有一个数组和文本文件类型输入

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

我只需要一个“h”、“e”、“o”和两次“l”按键输入到文本字段

这可能吗?

4

3 回答 3

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);
        }
    }
于 2013-10-30T21:58:25.423 回答
0

虽然您的问题相当神秘,但我会尽力回答。

TextFieldrestrict具有限制允许输入的内置属性。

这意味着您可以执行类似textField.restrict = "helo";的操作,并且仅允许用户输入字母 h、e、l 和 o。

这并没有解决您似乎希望用户只输入单词“你好”这一事实。为此,您必须在用户输入进入时捕获它并防止任何不需要的字符:

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

restrictionArray上面的代码通过和来跟踪接下来应该是哪个字母currentLetterIndex。任何不匹配的字母都不会TextField使用该preventDefault方法输入到 中。

于 2013-10-30T22:02:17.373 回答
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");
              }
    }
于 2013-11-03T10:29:08.847 回答