2013-01-22 85 views
0

我有一個可拖動的組件。但是,如果用戶在鼠標位於文本字段上時拖動它,則無法拖動它。如何設置文本字段,以便用戶可以在界面的該部分上單擊拖動?如何忽略鼠標事件

<?xml version="1.0" encoding="utf-8"?> 
<mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml" creationComplete="init()" width="210" height="150" styleName="noteStyle" > 
<mx:Script> 
    <![CDATA[ 
     public var testId:int; 
     public var buttonNumber:int; 
     public function init():void{ 
      this.addEventListener(MouseEvent.MOUSE_DOWN, makeDraggable) 
      this.addEventListener(MouseEvent.MOUSE_UP, endDrag) 
      this.buttonMode = true; 
      this.useHandCursor = true; 
     } 
     public function saveButtonHandler():void{ 
      dispatchEvent(new Event ("noteClosed")); 
     } 
     public function cancelButtonHandler():void{ 
      dispatchEvent(new Event ("noteCancelled")); 
     } 
     public function makeDraggable(e:Event):void{ 
      //If the target and the current target are the same, then it must be the background, so make it draggable. 
      //Stops the text field and buttons from being draggable 
      trace("Target= "+e.currentTarget+" "+e.target) 
      if(e.currentTarget==e.target){ 
       this.startDrag(); 
      } 
     } 
     public function endDrag(e:Event):void{ 
      this.stopDrag(); 
     } 
    ]]> 
</mx:Script> 
    <mx:Text text="Add a note: Drag to move" styleName="myriadPro14" selectable="false" /> 
    <mx:Button id="saveButton" label="Save" x="146" y="120" click="saveButtonHandler()" /> 
    <mx:Button id="cancelButton" label="Cancel" x="10" y="120" click="cancelButtonHandler()" /> 
    <mx:TextArea id="noteText" width="200" height="80"  horizontalCenter="0" verticalCenter="-12"/> 
</mx:Canvas> 
+0

textField.addEventListener(MouseEvent.MOUSE_DOWN,makeDraggable); textField.buttonMode = true; textField.useHandCursor = true; – SimonRH

回答

0

將事件監聽器添加到文本字段中。

+2

除了添加偵聽器,他應該使用'DisplayObjectContainer'上定義的'mouseChildren'屬性。如果設置爲false,則禁用鼠標和用戶輸入。 –

+0

這是正確的方法。 –

+0

@TimLieberman我遇到了一個類似的問題 - 我在MovieClip中有一個動態文本字段,並且當我將一個Mouse.CLICK事件偵聽器添加到影片剪輯時,它捕獲了文本字段實例而不是剪輯。我的解決方案是:textField.mouseEnabled = false,使文本字段忽略鼠標事件,並顯然傳遞它們。 – bks