HarmonyOS ArkUI-eTS通用事件监听

系统 OpenHarmony
本篇给大家带来ArkUI-eTS通用事件监听的相关知识,希望对你有所帮助!

​想了解更多内容,请访问:​

​51CTO和华为官方合作共建的鸿蒙技术社区​

​https://harmonyos.51cto.com​

1.点击事件

支持版本: eTS API Version 7+

事件名称: onClick(callback: (event?: ClickEvent) => void)

ClickEvent对象:

screenX(点击点相对于设备屏幕左边沿的X坐标)

screenY(点击点相对于设备屏幕右边沿的Y坐标)

x(点击点相对于被点击元素左边沿的X坐标)

y(点击点相对于被点击元素上边沿的Y坐标)

timestamp(事件时间戳)

target(API Version 8+ 支持此属性)/类型:EventTarget

使用示例:

@Entry
@Component
struct Sample {
  @State text: string = ''
  build() {
    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
      Button('点击按钮').backgroundColor(Color.Blue).width('50%').height(50)
        .onClick((event: ClickEvent) => {
          console.info(this.text = '点击点相对于屏幕位置:' + '\n  相对于屏幕左边X:' + event.screenX + '\n  相对于屏幕顶部Y:' + event.screenY
          + '\n所点击按钮:' + '\n  点击点相对于父元素坐标:\n (x:'
          + event.target.area.globalPos.x + ',y:' + event.target.area.globalPos.y + ')\n  所点击按钮宽度:'
          + event.target.area.width + '\n  所点击按钮高度:' + event.target.area.height)
        })
      Text(this.text).fontSize(15).padding(15)
    }
    .width('100%')
    .height('100%')
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.

效果演示:

2.触摸事件

支持版本: eTS API Version 7+

事件名称: onTouch(callback: (event?: TouchEvent) => void)

TouchEvent对象:

type(触摸事件的类型) / 类型:TouchType

–>TouchType.Down(手指按下时触发)

–>TouchType.Up(手指抬起时触发)

–>TouchType.Move(手指按压在屏幕上移动时触发)

–>TouchType.Cancel(触摸事件取消时触发)

touches(全部手指信息)

changedTouches(当前发生变化的手指信息)

timestamp(事件时间戳)

target(被触摸元素对象)–>此属性可参考上方点击事件中表格参数

使用示例:

@Entry
@Component
struct Sample {
  @State text: string = ''
  @State eventType: string=''
  build() {
    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
      Button('触摸按钮').backgroundColor(Color.Blue).width('70%').height(60)
      .onTouch((event:TouchEvent)=>{
        if(event.type == TouchType.Down){
          this.eventType = '按下'
        }
        if(event.type == TouchType.Up){
          this.eventType = '抬起'
        }
        if(event.type == TouchType.Move){
          this.eventType = '按下移动'
        }
        console.info(this.text = '触摸类型:' + this.eventType)
      })
      Text(this.text).fontSize(15).padding(15)
    }
    .width('100%')
    .height('100%')
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.

效果演示:

3.挂载卸载事件

支持版本: eTS API Version 7+

事件名称:

1.onAppear(callback: () => void)

2.onDisappear(callback: () => void)

关键操作: import prompt from ‘@system.prompt’

注意事项: 因需要读取设备系统信息,运行需要在模拟真机或真机运行

使用示例:

import prompt from '@system.prompt';
@Entry
@Component
struct Sample {
  private text: string = '挂载文本'
  @State isShow: boolean = true
  private changeAppear: string = '隐藏文本'
  build() {
    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
      Button(this.changeAppear)
        .backgroundColor(Color.Blue)
        .width(100)
        .height(60)
        .onClick(() => {
          this.isShow = !this.isShow
        })
      if (this.isShow) {
        Text(this.text)
          .fontSize(20)
        //挂载
          .onAppear(() => {
            this.changeAppear = '显示文本'
            prompt.showToast({ message: '文本显示', duration: 2000 })
          })
        //卸载
          .onDisAppear(() => {
            this.changeAppear = '隐藏文本'
            prompt.showToast({ message: '文本隐藏', duration: 2000 })
          })
      }
    }
    .width('100%')
    .height('100%')
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.

效果演示:

4.按键事件

支持版本: eTS API Version 7+

事件名称: onKeyEvent(event: (event?: KeyEvent) => void)

KeyEvent对象:

type(按键事件的类型) / 类型:KeyType

–>TouchType.Down(按下按键)

–>TouchType.Up(松开按键)

KeySource(触发当前按键的输入设备类型) / 类型:KeySource

–>KeySource.Unknown(输入设备类型未知)

–>KeySource.Keyboard(输入设备类型为键盘)

KeyCode(按键的键码)

注意事项: 因需要读取设备系统信息,运行需要在模拟真机或真机运行

使用示例:

@Entry
@Component
struct Sample {
  @State text: string = ''
  @State eventType: string = ''
  build() {
    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
      Button('按键')
        .backgroundColor(Color.Blue)
        .width(100)
        .height(60)
        .onKeyEvent((event: KeyEvent) => {
          if (event.type === KeyType.Down) {
            this.eventType = '按下'
          }
          if (event.type === KeyType.Up) {
            this.eventType = '松开'
          }
          console.info(this.text = '按键类型:' + this.eventType + '\n键码:' + event.keyCode + '\n键值:' + event.keyText)
        })
      Text(this.text).fontSize(15)
    }
    .width('100%')
    .height('100%')
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.

效果演示:

​想了解更多内容,请访问:​

​51CTO和华为官方合作共建的鸿蒙技术社区​

​https://harmonyos.51cto.com​

责任编辑:jianghua 来源: 鸿蒙社区
相关推荐

2022-02-23 15:07:22

HarmonyOS常用控制ArkUI-eTS

2022-07-05 16:13:37

ArkUI-eTS智能晾晒系统

2022-11-02 16:06:54

ArkUIETS

2022-10-24 14:49:54

ArkUI心电图组件

2023-04-09 14:48:03

JavaScript脚本语言开发

2011-11-21 16:49:58

日志常用事件

2022-09-05 15:22:27

ArkUIets

2022-07-04 16:34:46

流光按钮Stack

2009-09-03 17:59:18

C#调用事件

2022-01-25 17:05:44

ArkUI_eTS操作系统鸿蒙

2023-03-13 15:03:05

鸿蒙ArkUI

2022-08-12 19:13:07

etswifi连接操作

2023-06-21 08:00:00

微服务架构

2022-07-11 16:26:37

eTS计算鸿蒙

2022-05-26 14:50:15

ArkUITS扩展

2021-11-26 10:08:57

鸿蒙HarmonyOS应用

2021-12-23 09:00:00

架构微服务数据

2022-10-10 14:51:51

ArkUI eTSPieChart组件

2022-10-09 15:13:18

TextPickerArkUI eTS

2021-11-19 09:48:33

鸿蒙HarmonyOS应用
点赞
收藏

51CTO技术栈公众号