9

フレックスのターゲットと現在のターゲットの違いを教えてもらえますか?

4

3 に答える 3

18

確かに、私もこれに問題がありました。プロパティは、currentTargetイベントハンドラーを登録したIEventListenerです。これtargetは、現在処理しているイベントをディスパッチしたものです。したがって、currentTarget変更はありtargetません。

次の例を確認してください。

サンプルアプリ

<?xml version="1.0" encoding="utf-8"?>
<mx:Application
    xmlns:mx="http://www.adobe.com/2006/mxml"
    creationComplete="addListeners()">

    <mx:Script>
        <![CDATA[

            protected function addListeners():void
            {
                greatGrandParent.addEventListener(Event.COMPLETE, completeHandler);
                grandParent.addEventListener(Event.COMPLETE, completeHandler);
                aParent.addEventListener(Event.COMPLETE, completeHandler);
                child.addEventListener(Event.COMPLETE, completeHandler);
                // dispatch event that "bubbles", second param is "true"
                // dispatched from child
                child.dispatchEvent(new Event(Event.COMPLETE, true));
            }

            protected function completeHandler(event:Event):void
            {
                trace("target: ", event.target + ", currentTarget: ", event.currentTarget);
            }

        ]]>
    </mx:Script>

    <mx:Panel id="greatGrandParent">
        <mx:Panel id="grandParent">
            <mx:Panel id="aParent">
                <mx:Button id="child"/>
            </mx:Panel>
        </mx:Panel>
    </mx:Panel>

</mx:Application>

出力

target:  MyApp.greatGrandParent.grandParent.aParent.child, currentTarget:  MyApp.greatGrandParent.grandParent.aParent.child
target:  MyApp.greatGrandParent.grandParent.aParent.child, currentTarget:  MyApp.greatGrandParent.grandParent.aParent
target:  MyApp.greatGrandParent.grandParent.aParent.child, currentTarget:  MyApp.greatGrandParent.grandParent
target:  MyApp.greatGrandParent.grandParent.aParent.child, currentTarget:  MyApp.greatGrandParent

これは表示オブジェクトの単純なツリーであり、アプリの準備ができたら次のようにします。

  1. ツリーの各コンポーネントに同じイベントのリスナーを追加します。
  2. 任意のイベントをディスパッチします(デモンストレーション用)。私はを選びEvent.COMPLETEました。

すべてが同じイベントのeventHandlerを登録し、bubblestrue(new Event(type, bubbles))に設定したので、子からgreatGrandParent以降まで、のイベントハンドラーを登録したツリー内のすべてが、Event.COMPLETEそのメソッドを実行しますcompleteHandler。イベントはチェーンを上に移動し、次に下に移動します。これtargetはイベントをディスパッチしたものであるため、イベントをディスパッチしたため、child一定である必要があります。currentTarget何が変わるかです。

つまり、FlexでDataGridをロールオーバーするときにチェックしたい場合、DataGridのitemRendererの1つにあるチェックボックスをいつロールオーバーするかを知りたいということです。1つの方法は、のすべてのitemRendererのチェックボックスにEventListenerを追加することですMouseEvent.ROLL_OVER。もう1つの方法は、のDataGrid自体にEventListenerを追加し、イベントのターゲットMouseEvent.ROLL_OVERが何であるかを確認することです。

protected function dataGrid_rollOverHandler(event:MouseEvent):void
{
    // event.currentTarget is DataGrid
    if (event.target is CheckBox)
        trace("rolled over checkbox!");
}

それが私がよく使う方法event.targetです。

お役に立てば幸い、ランス

于 2010-02-25T13:18:19.743 に答える
1

したがって、役立つ可能性があります:

http://livedocs.adobe.com/flex/3/html/help.html?content=events_08.html#219548

于 2010-02-25T13:20:12.193 に答える
-9

このような質問をする前に、次のサイトのチュートリアル(http://www.adobe.com/devnet/flex/videotraining/ )でFlexの概要を確認する必要があります。あなたの質問は1日目にカバーされます。

于 2010-02-25T21:54:08.543 に答える