ts-universal-events-click.md 2.7 KB
Newer Older
Z
zengyawen 已提交
1
# Click Event
Z
zengyawen 已提交
2 3


E
esterzhou 已提交
4
> ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE**
Z
zengyawen 已提交
5 6 7 8
> This method is supported since API version 7. Updates will be marked with a superscript to indicate their earliest API version.


## Required Permissions
Z
zengyawen 已提交
9 10 11

None

Z
zengyawen 已提交
12 13 14

## Events

E
esterzhou 已提交
15
  | Name | Bubble Supported | Description | 
Z
zengyawen 已提交
16
| -------- | -------- | -------- |
E
esterzhou 已提交
17
| onClick(callback: (event?: ClickEvent) => void) | No | Called when a click event occurs. For details about the event parameters, see [ClickEvent](#clickevent). | 
Z
zengyawen 已提交
18 19 20 21 22 23


### ClickEvent

  | Name | Type | Description | 
| -------- | -------- | -------- |
E
esterzhou 已提交
24 25 26 27 28 29
| screenX | number | X coordinate of the click relative to the left edge of the screen. | 
| screenY | number | Y coordinate of the click relative to the upper edge of the screen. | 
| x | number | X coordinate of the click relative to the left edge of the component being clicked. | 
| y | number | Y coordinate of the click relative to the upper edge of the component being clicked. | 
| target<sup>8+</sup> | EventTarget | Target element that is clicked. | 
| timestamp | number | Timestamp of the event. | 
Z
zengyawen 已提交
30

31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
- EventTarget<sup>8+</sup> attributes

    | Name | Type | Description | 
    | -------- | -------- | -------- |
    | area | Area | Area information of the target element.| 

- Area<sup>8+</sup> attributes

    | Name | Type | Description | 
    | -------- | -------- | -------- |
    | width | number | Width of the target element, in vp. | 
    | height | number | Height of the target element, in vp. | 
    | pos | Position | Position of the upper left corner of the target element relative to that of the parent element. | 
    | globalPos | Position | Position of the upper left corner of the target element relative to that of the page. | 

- Position<sup>8+</sup> attributes

    | Name | Type | Description | 
    | -------- | -------- | -------- |
    | x | number | X-coordinate, in vp. | 
    | y | number | Y-coordinate, in vp. | 

Z
zengyawen 已提交
53 54 55

## Example

Z
zengyawen 已提交
56 57 58 59 60 61 62 63 64

```
@Entry
@Component
struct ClickExample {
  @State text: string = ''

  build() {
    Column() {
Z
zengyawen 已提交
65
      Button('Click').backgroundColor(0x2788D9).width(100).height(40)
Z
zengyawen 已提交
66
        .onClick((event: ClickEvent) => {
Z
zengyawen 已提交
67
          console.info(this.text = 'Click Point:' + '\n  screenX:' + event.screenX + '\n  screenY:' + event.screenY
Z
zengyawen 已提交
68 69 70
          + '\n  x:' + event.x + '\n  y:' + event.y + '\ntarget:' + '\n  component globalPos:('
          + event.target.area.globalPos.x + ',' + event.target.area.globalPos.y + ')\n  width:'
          + event.target.area.width + '\n  height:' + event.target.area.height)
Z
zengyawen 已提交
71 72
        })
      Text(this.text).padding(15)
Z
zengyawen 已提交
73
    }.height(350).width('100%').padding(10)
Z
zengyawen 已提交
74 75 76 77 78
  }
}
```


Z
zengyawen 已提交
79
![en-us_image_0000001256858383](figures/en-us_image_0000001256858383.gif)