开发者

Set Selection in AS3

Ho开发者_JAVA技巧w do I get Set Selection to work in a text field. I have a text field inside a movieclip and a button with a click listener on it. When it clicks I want it to select all the text inside. This is what I have so far, I hope you can help.

send.addEventListener(MouseEvent.CLICK, function() {
    panel.tweet.selectable = true;
    stage.focus = panel.tweet;
    panel.tweet.setSelection(0, panel.tweet.text.length);
});


Crazy - should work fine.

I made a little demo for you to see it working:

http://strangemother.com/actionscript/demos/select_text_click_demo/

import flash.events.MouseEvent;

send.addEventListener(MouseEvent.CLICK, sendMouseClickEventHandler);

function sendMouseClickEventHandler(ev:MouseEvent):void
{
    stage.focus = tweet;
    tweet.selectable = true;
    tweet.setSelection(0, tweet.text.length ); 

}


There is a better solution: use callLater; the problem was the mouseEvent. If you try to execute the selection later, it works.


Try doing it this way: (I don't have Flash right now to test...)

send.addEventListener(MouseEvent.CLICK, function() {
    panel.tweet.selectable = true;
    panel.tweet.stage.focus = panel.tweet;
    panel.tweet.setSelection(0, panel.tweet.text.length);
});

If it's not this, then it could be the mouseup event that's clobbering the highlight.(?)


Your code should work, I think.

Is the event listener being called at all?

Maybe there's some movieclip over it that blocks your send button (it could be transparent and you might not realize it's there).

A quick and dirty way to check if that's the problem:

send.stage.addChild(send);

This will place your button on top of every object. If your handler was not called and after doing that gets called, you can be pretty sure there's something blocking it. If that case, you could re-arrange depths or try setting blocking movieclips mouseEnabled property to false. mouseChildren could also help if said movieclips contain in turn other blocking objects (that don't need to react to mouse events, of course).


The problem is with the focus.

Just do this (introduce a delay of 10 ms)

var timer1:Timer = new Timer(10,1);
timer1.addEventListener(TimerEvent.TIMER, delayedSelection);
timer1.start();

function delayedSelection(e:TimerEvent):void
{
    stage.focus = tweet;
    tweet.selectable = true;
    tweet.setSelection(0, tweet.text.length ); 
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜