harmony 鸿蒙Click Event

2022-08-09 浏览 (1049)

Click Event

A click event is triggered when a component is clicked.

NOTE

The APIs of this module are supported since API version 7. Updates will be marked with a superscript to indicate their earliest API version.

Events

NameBubbling SupportedDescription
onClick(event: (event?: ClickEvent) => void)NoCalled when a click event occurs. For details about event, see ClickEvent.
Since API version 9, this API is supported in ArkTS widgets.

ClickEvent

Since API version 9, this API is supported in ArkTS widgets.

NameTypeDescription
xnumberX coordinate of the click relative to the upper left corner of the component being clicked.
ynumberY coordinate of the click relative to the upper left corner of the component being clicked.
timestamp8+numberTimestamp of the event. It is the interval between the time when the event is triggered and the time when the system starts, in nanoseconds.
target8+EventTargetDisplay area of the object that triggers the event.
source8+SourceTypeEvent input device.
windowX10+numberX coordinate of the click relative to the upper left corner of the application window.
windowY10+numberY coordinate of the click relative to the upper left corner of the application window.
displayX10+numberX coordinate of the click relative to the upper left corner of the application screen.
displayY10+numberY coordinate of the click relative to the upper left corner of the application screen.
screenX(deprecated)numberX coordinate of the click relative to the upper left corner of the application window.
This API is deprecated since API version 10. You are advised to use windowX instead.
screenY(deprecated)numberY coordinate of the click relative to the upper left corner of the application window.
This API is deprecated since API version 10. You are advised to use windowY instead.

EventTarget8+

Since API version 9, this API is supported in ArkTS widgets.

NameTypeDescription
areaAreaArea information of the target element.

Example

// xxx.ets
@Entry
@Component
struct ClickExample {
  @State text: string = ''

  build() {
    Column() {
      Row({ space: 20 }) {
        Button('Click').width(100).height(40)
          .onClick((event?: ClickEvent) => {
            if(event){
              this.text = 'Click Point:' + '\n  windowX:' + event.windowX + '\n  windowY:' + event.windowY
              + '\n  x:' + event.x + '\n  y:' + event.y + '\ntarget:' + '\n  component globalPos:('
              + event.target.area.globalPosition.x + ',' + event.target.area.globalPosition.y + ')\n  width:'
              + event.target.area.width + '\n  height:' + event.target.area.height + '\ntimestamp' + event.timestamp;
            }
          })
        Button('Click').width(200).height(50)
          .onClick((event?: ClickEvent) => {
            if(event){
              this.text = 'Click Point:' + '\n  windowX:' + event.windowX + '\n  windowY:' + event.windowY
              + '\n  x:' + event.x + '\n  y:' + event.y + '\ntarget:' + '\n  component globalPos:('
              + event.target.area.globalPosition.x + ',' + event.target.area.globalPosition.y + ')\n  width:'
              + event.target.area.width + '\n  height:' + event.target.area.height + '\ntimestamp' + event.timestamp;
            }
          })
      }.margin(20)

      Text(this.text).margin(15)
    }.width('100%')
  }
}

en-us_image_0000001210353788

你可能感兴趣的鸿蒙文章

harmony 鸿蒙ArkTS-based Declarative Development Paradigm

harmony 鸿蒙@ohos.multimedia.avCastPicker (AVCastPicker)

harmony 鸿蒙Property Animation

harmony 鸿蒙Enums

harmony 鸿蒙Blank

harmony 鸿蒙Button

harmony 鸿蒙CalendarPicker

harmony 鸿蒙Checkbox

harmony 鸿蒙CheckboxGroup

harmony 鸿蒙DataPanel

  • 所属分类: 后端技术
  • 本文标签: 鸿蒙 软件
  • 版权声明: 本文链接 https://seaxiang.com/blog/34e81e5da026420e8f4a1ef06d3b05fc