2012-07-06 85 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;,則所有內容都應按預期工作。