开发者

How set the focus of a input-TextField without having a stagereference?

I got a text-InputField:

var textfield:TextField = new TextField();
textfield.text = "";
textfield.type = TextFieldType.INPUT;
addChild(textfield);

because this textfield don't reside in the main-class, I don't have a stage-reference for doing:

textfield.stage.focus = text开发者_如何学JAVAfield;
or 
stage.focus = textfield

How can I force the textfield to display the blinking line at position zero?


You can add a static reference to the stage in your main class and set focus through it (or create a dedicaced class to get stage reference wherever you are).

Main class code :

public class MainClass extends Sprite{
    public static var STAGE : Stage;

    public function MainClass(){
        STAGE = stage;
    }
}

Textfield class code :

MainClass.STAGE.focus = myTextfield;


It's sort of a flash player bug, because if you set focus with stage.focus=textfield, textfield actually has focus (you can write something), but there is no blinking cursor. On the other hand, there is a blinking cursor, but only when you launch swf locally in player (not in browser). To inform people that they can write something right now, you can make a cursor by yourself - just create a line, animate it and hide when someone writes a first letter.


The cursor focus does work sometimes, which is all the more frustrating. I have this situation with two almost identical apps. The one that doesn't work has an animation running. It may be a conflict.


Finally I got the solution

try this code it will work

public function SetFocus(mc:TextField):void{            
    addEventListener(Event.ENTER_FRAME,focus);
    function focus(E:Event){
        stage.focus=mc;
        if(mc.name.toString()==stage.focus.name.toString()){
            removeEventListener(Event.ENTER_FRAME,focus);   
        }
    }
}

you have to name the textfield then you can use it easily

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜