DEV Community

魔眼天王
魔眼天王

Posted on

Comprehensive Guide to Event Dispatching in HarmonyOS

Note: HarmonyOS APIs evolve rapidly. If discrepancies arise between this guide and current implementations, please refer to the official latest documentation.

I. Event Delivery Process

HarmonyOS employs a ​capture-target-bubble​ three-stage event delivery model:

graph LR
A[Event Capture] --> B{Target Component}
B --> C[Event Bubble]
Enter fullscreen mode Exit fullscreen mode
  • ​Capture Phase: Parent components handle events first

  • ​Target Phase: Actual target component processes the event

  • ​Bubble Phase: Events propagate upward to parent components

II. API Specifications

// Parent component blocking child events (Key API)
Component.setHitTestBehavior(HitTestMode.Block);

// Child component click binding
Button("Click")
  .onClick(() => {
    console.log("Button clicked");
  });
Enter fullscreen mode Exit fullscreen mode

​*

III. Common Conflict Scenarios & Solutions

Scenario 1: Parent-Child Click Conflicts

​Symptoms:

  • Parent has click listener

  • Child button click triggers parent event

​Solution:

// Parent.ets
@Entry
@Component
struct Parent {
  build() {
    Column() {
      // Block child touch propagation
      Stack().setHitTestBehavior(HitTestMode.Block)
        .child(Button("Child Button").onClick(() => {
          console.log("Child button clicked");
        }))
        .onClick(() => {
          console.log("Parent clicked");
        })
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

Prevents touch propagation to children via HitTestMode.Block

Scenario 2: Swipe vs Click Conflicts

​Symptoms:

  • List items require both click and swipe

  • Clicks accidentally trigger swipes

​Solution:

// ListItem.ets
@Component
struct ListItem {
  private isSwiping = false;

  build() {
    Column()
      .onTouch((event) => {
        if(event.type === TouchType.Move) {
          this.isSwiping = true;
        }
        return this.isSwiping;
      })
      .onClick(() => {
        if(!this.isSwiping) {
          console.log("Valid click");
        }
      })
  }
}
Enter fullscreen mode Exit fullscreen mode

Distinguishes clicks from swipes using touch event phases

​*

IV. Event Priority Control

1. Native Event Priority Levels

Event Type Priority Description
System-level events Highest Back button, system dialogs
Gesture recognition High Swipe, long-press
Component native Medium Button.onClick
Custom events Lowest Developer-defined events

2. Gesture Conflict Resolution

// Dual tap + long press support
Button("Action")
  .onClick(() => console.log("Click"))
  .onLongPress(() => console.log("Long press"))
  .gesture(
    GestureGroup(
      Gesture.Tap({ count: 2 }),  // Double-tap
      Gesture.LongPress()         // Long press
    )
  );
Enter fullscreen mode Exit fullscreen mode

Uses GestureGroup for mutually exclusive gesture recognition

​*

V. Debugging & Validation

1. Event Logging

// Component initialization
Component.onInit(() => {
  this.onClick = (event) => {
    console.log(`[${this.type}] Click at (${event.x},${event.y})`);
  }
});
Enter fullscreen mode Exit fullscreen mode

2. Visual Debugging Tools

  1. Enable DevEco Studio's ​Layout Inspector​

  2. Activate ​Enable Touch Test​ option

  3. Visualize component touch hotspots in real-time

​*

VI. Recommended Implementation Practices

1. Single Responsibility Principle

// Component handles single event type
@Component
struct SafeZone {
  @State isTouched = false;

  build() {
    Box()
      .onTouch((e) => this.isTouched = e.type === TouchType.Down)
      .onClick(() => {
        if(!this.isTouched) handleClick();
      })
  }
}
Enter fullscreen mode Exit fullscreen mode

2. Event Delegation Pattern

// Parent handles child events
@Component
struct ListContainer {
  private items: Array<ItemData> = [];

  build() {
    List() {
      ForEach(this.items, (item) => {
        ListItem(item)
          .onClick((e) => this.handleItemClick(item))
      })
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

​*

VII. Key API Cross-Reference

Functionality Correct API Documentation
Click binding .onClick() [Component Events]
Touch control .setHitTestBehavior() [HitTest Documentation]
Gesture recognition Gesture.Tap() [Gesture API]
Bubble control event.stopPropagation() [Event Propagation]

Note: For complete API specifications, refer to HarmonyOS Official Documentation.

This translation maintains technical accuracy while adapting to English technical documentation conventions. Key architectural concepts and implementation patterns are preserved to ensure developers can effectively implement event-driven architectures in HarmonyOS applications.

Top comments (0)