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


Z
zengyawen 已提交
4 5 6 7 8
> ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE:**
> 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 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33

## Events

  | Name | Bubble Supported | Description | 
| -------- | -------- | -------- |
| onClick(callback: (event?: ClickEvent) => void) | No | Called when a click event occurs. For details about the event parameters, see [ClickEvent](#clickevent). | 


### ClickEvent

  | Name | Type | Description | 
| -------- | -------- | -------- |
| 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&nbsp;element&nbsp;that&nbsp;is&nbsp;clicked. | 
| timestamp | number | Timestamp&nbsp;of&nbsp;the&nbsp;event. | 


## 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)