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

E
ester.zhou 已提交
3
A click event is triggered when a component is clicked.
Z
zengyawen 已提交
4

E
ester.zhou 已提交
5
>  **NOTE**
E
ester.zhou 已提交
6
>
E
ester.zhou 已提交
7
>  The APIs of this module are supported since API version 7. Updates will be marked with a superscript to indicate their earliest API version.
Z
zengyawen 已提交
8

Z
zengyawen 已提交
9 10 11

## Events

E
ester.zhou 已提交
12 13 14
| Name                                      | Bubbling Supported| Description                             |
| ---------------------------------------- | ---- | --------------------------------- |
| onClick(event: (event?: ClickEvent) => void) | No   | Called when a click event occurs. For details about the event parameters, see **ClickEvent**.|
Z
zengyawen 已提交
15

E
ester.zhou 已提交
16
## ClickEvent
E
ester.zhou 已提交
17 18 19 20 21 22 23 24
| Name           | Type                                | Description                                                    |
| ------------------- | ------------------------------------ | -------------------------------------------------------- |
| screenX             | number                               | X coordinate of the click relative to the upper left corner of the application window.                     |
| screenY             | number                               | Y coordinate of the click relative to the upper left corner of the application window.                     |
| x                   | number                               | X coordinate of the click relative to the upper left corner of the component being clicked.                   |
| y                   | number                               | Y coordinate of the click relative to the upper left corner of the component being clicked.                   |
| target<sup>8+</sup> | [EventTarget](#eventtarget8) | Target element that is clicked.                                        |
| timestamp           | number                               | Timestamp of the event. It is interval between the time when the event is triggered and the time when the system starts, in nanoseconds.|
Z
zengyawen 已提交
25

E
ester.zhou 已提交
26
## EventTarget<sup>8+</sup>
27

E
ester.zhou 已提交
28 29 30
| Name  | Type                     | Description        |
| ---- | ------------------------- | ---------- |
| area | [Area](ts-types.md#area8) | Area information of the target element.|
31 32


Z
zengyawen 已提交
33 34 35

## Example

E
ester.zhou 已提交
36 37
```ts
// xxx.ets
Z
zengyawen 已提交
38 39 40 41 42 43 44
@Entry
@Component
struct ClickExample {
  @State text: string = ''

  build() {
    Column() {
Z
zengyawen 已提交
45
      Button('Click').backgroundColor(0x2788D9).width(100).height(40)
Z
zengyawen 已提交
46
        .onClick((event: ClickEvent) => {
Z
zengyawen 已提交
47
          console.info(this.text = 'Click Point:' + '\n  screenX:' + event.screenX + '\n  screenY:' + event.screenY
Z
zengyawen 已提交
48
          + '\n  x:' + event.x + '\n  y:' + event.y + '\ntarget:' + '\n  component globalPos:('
E
ester.zhou 已提交
49
          + event.target.area.globalPosition.x + ',' + event.target.area.globalPosition.y + ')\n  width:'
Z
zengyawen 已提交
50
          + event.target.area.width + '\n  height:' + event.target.area.height)
Z
zengyawen 已提交
51 52
        })
      Text(this.text).padding(15)
Z
zengyawen 已提交
53
    }.height(350).width('100%').padding(10)
Z
zengyawen 已提交
54 55 56 57 58
  }
}
```


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