ts-universal-events-click.md 1.8 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


## Example

Z
zengyawen 已提交
34 35 36 37 38 39 40 41 42

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

  build() {
    Column() {
Z
zengyawen 已提交
43
      Button('Click').backgroundColor(0x2788D9).width(100).height(40)
Z
zengyawen 已提交
44
        .onClick((event: ClickEvent) => {
Z
zengyawen 已提交
45
          console.info(this.text = 'Click Point:' + '\n  screenX:' + event.screenX + '\n  screenY:' + event.screenY
Z
zengyawen 已提交
46 47 48
          + '\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 已提交
49 50
        })
      Text(this.text).padding(15)
Z
zengyawen 已提交
51
    }.height(350).width('100%').padding(10)
Z
zengyawen 已提交
52 53 54 55 56
  }
}
```


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