2012-07-06 82 views
0

我想在按钮内部有一个TextInput,但似乎我的按钮拦截了我的TextInput之前的所有鼠标事件。TextInput永远不会获得焦点

基本上我有一个类BaseButton这样的:

public class BaseButton extends Sprite 
{ 
    [...] 
     public function addMouseListeners(target : InteractiveObject = null) : void { 
      if (!target) target = this; 
      target.addEventListener(MouseEvent.ROLL_OVER, mouseOverHandler, false, 0, true); 
      target.addEventListener(MouseEvent.ROLL_OUT, mouseOutHandler, false, 0, true); 
      target.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler, false, 0, true); 
      target.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler, false, 0, true); 
     } 
    [...] 
} 

,我想把类TextInput扩展BaseButton

public class AddFilterDropDownListItem extends BaseButton { 
    private var _input:TextInput; 

    public function AddFilterDropDownListItem() { 
     super(); 
    } 

    override protected function setupAssets():void { 
     _input = new TextInput(); 
     _input.height = 40; 
     _input.width = 240; 
     this.addChild(_input); 
     _input.appendText("Add.."); 
    } 
} 

我不能编辑TextInput,单击事件似乎是被BaseButton捕获。我不明白为什么,作为BaseButton的小孩,我的TextInput没有优先权?

我可以将它添加到父解决通过添加TextInput我的问题:

override protected function setupAssets():void { 
    _input = new TextInput(); 
    _input.height = 40; 
    _input.width = 240; 
    parent.addChild(_input); //<------ dirty solution 
    _input.appendText("Add.."); 
} 

你能解释我为什么会失败?我该如何干净地解决它? PS:我不能改变项目的体系结构。我的班级必须延长BaseButton,我不能更改BaseButton

+0

您可能必须显式设置'mouseChildren = true;',具体取决于'BaseButton'类默认的功能。我不太明白你为什么要在按钮中输入文字,但是 - 从用户界面的角度来看,这对我来说并不合适。 – weltraumpirat 2012-07-06 06:57:37

+0

@weltraumpirat对我来说也没什么意义......我是这个项目的新手,并且第一次使用as3,但我已经可以说项目代码太可怕了......无论如何,你的解决方案就像一个魅力!谢谢。请做出回答,以便我可以将其标记为已接受;) – tibo 2012-07-06 07:23:55

+0

当然可以:)不客气。 – weltraumpirat 2012-07-06 07:38:01

回答

1

BaseButton类可能有mouseChildren设置为false,它可以防止任何嵌套的DisplayObject接收鼠标事件。如果您在派生类中设置了mouseChildren = true;,则所有内容都应按预期工作。