当前位置: 首页 > news >正文

【ArkTS-通用事件】

ArkTS-通用事件

  • ■ 基础输入事件
    • ■ 1. onTouch 触摸事件
    • ■ 2. onHover 鼠标悬浮
    • ■ 3. onMouse 鼠标事件
    • ■ 4. onAxisEvent 轴事件
    • ■ 5. onKeyEvent 按键事件
    • ■ 6. onDigitalCrown 表冠事件
    • ■ 7. onFocusAxisEvent 焦点轴事件
  • ■ 交互响应事件
    • ■ 1. onClick 点击事件
    • ■ 2. 拖拽事件
    • ■ 3. 焦点事件
    • ■ 4. 悬浮事件
    • ■ 5. 交互事件分发控制
    • ■ 6. 组件变化事件-挂载卸载事件
    • ■ 7. 组件变化事件
      • ■ onAreaChange 组件区域变化事件
      • ■ onSizeChange 组件尺寸变化事件
      • ■ onVisibleAreaChange 组件可见区域变化事件
        • ■ 示例1 (使用onVisibleAreaChange来监听区域变化)
        • ■ 示例2 (使用onVisibleAreaApproximateChange来监听区域变化)
  • ■ 设置事件回调
    • ■ 1. setOnClick
    • ■ 2. setOnTouch
    • ■ 3. setOnAppear
    • ■ 4. setOnDisappear
    • ■ 5. setOnKeyEvent
    • ■ 6. setOnFocus
    • ■ 7. setOnBlur
    • ■ 8. setOnHover
    • ■ 9. setOnMouse
    • ■ 10. setOnSizeChange
    • ■ 11. setOnVisibleAreaApproximateChange
    • ■ 12. HoverCallback

■ 基础输入事件

在这里插入图片描述
在这里插入图片描述

■ 1. onTouch 触摸事件

■ 2. onHover 鼠标悬浮

■ 3. onMouse 鼠标事件

■ 4. onAxisEvent 轴事件

轴事件指组件被鼠标滚轮滚动或触控板双指沿特定方向(轴)滑动时触发的事件。“轴”指二维坐标系中的方向,分为水平(X轴)和垂直(Y轴)。

■ 5. onKeyEvent 按键事件

是指组件与键盘、遥控器等按键设备交互时触发的事件,适用于所有可获焦组件,例如Button。对于默认不可获焦的组件,如Text,Image等,可以将focusable属性设置为true后使用按键事件。

■ 6. onDigitalCrown 表冠事件

■ 7. onFocusAxisEvent 焦点轴事件

■ 交互响应事件

■ 1. onClick 点击事件

点击动作触发该回调。

// xxx.ets
@Entry
@Component
struct ClickExample {@State text: string = '';build() {Column() {Row({ space: 20 }) {Button('Click').width(100).height(40).id('click1').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 + '\n  id:'+ event.target.id+ '\ntargetDisplayId:' + event.targetDisplayId + '\ntimestamp' + event.timestamp;}}, 20)Button('Click').width(200).height(50).id('click2').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 + '\n  id:'+ event.target.id+ '\ntargetDisplayId:' + event.targetDisplayId + '\ntimestamp' + event.timestamp;}}, 20)}.margin(20)Text(this.text).margin(15)}.width('100%')}
}

■ 2. 拖拽事件

拖拽事件是指在用户界面中,当用户拖动某个对象(如文件、控件或元素)时触发的一系列事件。
这些事件允许开发者自定义拖拽行为,实现诸如拖放、调整位置等功能。

属性描述
onDragStart
onDragEnter拖拽进入组件范围内时,触发回调,当监听了onDrop事件时,此事件才有效。
onDragMove拖拽在组件范围内移动时,触发回调,当监听了onDrop事件时,此事件才有效。
onDragLeave拖拽离开组件范围内时,触发回调,当监听了onDrop事件时,此事件才有效。
onDrop
onDragEnd
onPreDrag

■ 3. 焦点事件

当前组件获取焦点时触发的回调。

属性描述
onFocus焦点事件
onBlur失去焦点时触发

■ 4. 悬浮事件

属性描述
onHover鼠标或手写笔进入或退出组件时,触发hover事件。
onPreDrag

■ 5. 交互事件分发控制

属性描述
onTouchIntercept自定义事件拦截, 给组件绑定自定义事件拦截回调。
onChildTouchTest自定义事件分发, 当前组件通过设置回调,可自定义触摸测试并控制触摸测试中的子节点行为。

■ 6. 组件变化事件-挂载卸载事件

属性描述
onAttach组件挂载到组件树时触发此回调。
onDetach组件从组件树卸载时触发此回调。
onAppear组件挂载显示后触发此回调。
onDisAppear组件从组件树卸载消失时触发此回调。

■ 7. 组件变化事件

■ onAreaChange 组件区域变化事件

组件区域变化时触发该回调。仅会响应由布局变化所导致的组件大小、位置发生变化时的回调

// xxx.ets
@Entry
@Component
struct AreaExample {@State value: string = 'Text'@State sizeValue: string = ''build() {Column() {Text(this.value).backgroundColor(Color.Green).margin(30).fontSize(20).onClick(() => {this.value = this.value + 'Text'}).onAreaChange((oldValue: Area, newValue: Area) => {console.info(`Ace: on area change, oldValue is ${JSON.stringify(oldValue)} value is ${JSON.stringify(newValue)}`)this.sizeValue = JSON.stringify(newValue)})Text('new area is: \n' + this.sizeValue).margin({ right: 30, left: 30 })}.width('100%').height('100%').margin({ top: 30 })}
}

■ onSizeChange 组件尺寸变化事件

// xxx.ets
@Entry
@Component
struct AreaExample {@State value: string = 'Text'@State sizeValue: string = ''build() {Column() {Text(this.value).backgroundColor(Color.Green).margin(30).fontSize(20).onClick(() => {this.value = this.value + 'Text'}).onSizeChange((oldValue: SizeOptions, newValue: SizeOptions) => {console.info(`Ace: on size change, oldValue is ${JSON.stringify(oldValue)} value is ${JSON.stringify(newValue)}`)this.sizeValue = JSON.stringify(newValue)})Text('new area is: \n' + this.sizeValue).margin({ right: 30, left: 30 })}.width('100%').height('100%').margin({ top: 30 })}
}

■ onVisibleAreaChange 组件可见区域变化事件

■ 示例1 (使用onVisibleAreaChange来监听区域变化)
// xxx.ets
@Entry
@Component
struct ScrollExample {scroller: Scroller = new Scroller()private arr: number[] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]@State testTextStr: string = 'test'@State testRowStr: string = 'test'build() {Column() {Column() {Text(this.testTextStr).fontSize(20)Text(this.testRowStr).fontSize(20)}.height(100).backgroundColor(Color.Gray).opacity(0.3)Scroll(this.scroller) {Column() {Text("Test Text Visible Change").fontSize(20).height(200).margin({ top: 50, bottom: 20 }).backgroundColor(Color.Green)// 通过设置ratios为[0.0, 1.0],实现当组件完全显示或完全消失在屏幕中时触发回调。.onVisibleAreaChange([0.0, 1.0], (isExpanding: boolean, currentRatio: number) => {console.info('Test Text isExpanding: ' + isExpanding + ', currentRatio:' + currentRatio)if (isExpanding && currentRatio >= 1.0) {console.info('Test Text is fully visible. currentRatio:' + currentRatio)this.testTextStr = 'Test Text is fully visible'}if (!isExpanding && currentRatio <= 0.0) {console.info('Test Text is completely invisible.')this.testTextStr = 'Test Text is completely invisible'}})Row() {Text('Test Row Visible  Change').fontSize(20).margin({ bottom: 20 })}.height(200).backgroundColor(Color.Yellow).onVisibleAreaChange([0.0, 1.0], (isExpanding: boolean, currentRatio: number) => {console.info('Test Row isExpanding:' + isExpanding + ', currentRatio:' + currentRatio)if (isExpanding && currentRatio >= 1.0) {console.info('Test Row is fully visible.')this.testRowStr = 'Test Row is fully visible'}if (!isExpanding && currentRatio <= 0.0) {console.info('Test Row is completely invisible.')this.testRowStr = 'Test Row is completely invisible'}})ForEach(this.arr, (item:number) => {Text(item.toString()).width('90%').height(150).backgroundColor(0xFFFFFF).borderRadius(15).fontSize(16).textAlign(TextAlign.Center).margin({ top: 10 })}, (item:number) => (item.toString()))}.width('100%')}.backgroundColor(0x317aff).scrollable(ScrollDirection.Vertical).scrollBar(BarState.On).scrollBarColor(Color.Gray).scrollBarWidth(10).onWillScroll((xOffset: number, yOffset: number, scrollState: ScrollState) => {console.info(xOffset + ' ' + yOffset)}).onScrollEdge((side: Edge) => {console.info('To the edge')}).onScrollStop(() => {console.info('Scroll Stop')})}.width('100%').height('100%').backgroundColor(0xDCDCDC)}
}
■ 示例2 (使用onVisibleAreaApproximateChange来监听区域变化)
// xxx.ets
@Entry
@Component
struct ScrollExample {scroller: Scroller = new Scroller()private arr: number[] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]@State testTextStr: string = 'test'@State testRowStr: string = 'test'build() {Column() {Column() {Text(this.testTextStr).fontSize(20)Text(this.testRowStr).fontSize(20)}.height(100).backgroundColor(Color.Gray).opacity(0.3)Scroll(this.scroller) {Column() {Text("Test Text Visible Change").fontSize(20).height(200).margin({ top: 50, bottom: 20 }).backgroundColor(Color.Green)// 通过设置ratios为[0.0, 1.0],实现当组件完全显示或完全消失在屏幕中时触发回调。.onVisibleAreaApproximateChange({ratios: [0.0, 1.0], expectedUpdateInterval: 1000}, (isExpanding: boolean, currentRatio: number) => {console.info('Test Text isExpanding: ' + isExpanding + ', currentRatio:' + currentRatio)if (isExpanding && currentRatio >= 1.0) {console.info('Test Text is fully visible. currentRatio:' + currentRatio)this.testTextStr = 'Test Text is fully visible'}if (!isExpanding && currentRatio <= 0.0) {console.info('Test Text is completely invisible.')this.testTextStr = 'Test Text is completely invisible'}})Row() {Text('Test Row Visible  Change').fontSize(20).margin({ bottom: 20 })}.height(200).backgroundColor(Color.Yellow).onVisibleAreaChange([0.0, 1.0], (isExpanding: boolean, currentRatio: number) => {console.info('Test Row isExpanding:' + isExpanding + ', currentRatio:' + currentRatio)if (isExpanding && currentRatio >= 1.0) {console.info('Test Row is fully visible.')this.testRowStr = 'Test Row is fully visible'}if (!isExpanding && currentRatio <= 0.0) {console.info('Test Row is completely invisible.')this.testRowStr = 'Test Row is completely invisible'}})ForEach(this.arr, (item:number) => {Text(item.toString()).width('90%').height(150).backgroundColor(0xFFFFFF).borderRadius(15).fontSize(16).textAlign(TextAlign.Center).margin({ top: 10 })}, (item:number) => (item.toString()))}.width('100%')}.backgroundColor(0x317aff).scrollable(ScrollDirection.Vertical).scrollBar(BarState.On).scrollBarColor(Color.Gray).scrollBarWidth(10).onWillScroll((xOffset: number, yOffset: number, scrollState: ScrollState) => {console.info(xOffset + ' ' + yOffset)}).onScrollEdge((side: Edge) => {console.info('To the edge')}).onScrollStop(() => {console.info('Scroll Stop')})}.width('100%').height('100%').backgroundColor(0xDCDCDC)}
}

■ 设置事件回调

设置事件回调

■ 1. setOnClick

■ 2. setOnTouch

■ 3. setOnAppear

■ 4. setOnDisappear

■ 5. setOnKeyEvent

■ 6. setOnFocus

■ 7. setOnBlur

■ 8. setOnHover

■ 9. setOnMouse

■ 10. setOnSizeChange

■ 11. setOnVisibleAreaApproximateChange

■ 12. HoverCallback

http://www.dtcms.com/a/461174.html

相关文章:

  • 为什么要放弃用Docker?
  • 东方仙盟修仙(五)赛博科技修仙养老是一种爱好
  • 大模型训练过程中主动学习和课程学习的全面对比
  • Redis数据库隔离业务缓存对查询性能的影响分析
  • Mapper接口的解析
  • 网站中链接怎么做的广东网站建设案例
  • 网站开发部门结构公司做网站的费用属于什么费用
  • 做电影方面的网站怎么做外贸免费平台
  • transformers音频实战02-基于 Speech Commands 数据集的语音识别实战项目全流程
  • 天津做网站美工锦州网站建设信息
  • Terraform + RustFS 实战:10分钟实现对象存储 IaC 化,运维效率提升300%
  • 遇到RabbitMQ 的 `channel_max` 限制报错
  • 机器学习实战项目:Python+Flask 汽车销量分析可视化系统(requests爬车主之家+可视化 源码+文档)✅
  • 惠州城乡建设部网站印刷包装公司网站模板
  • WEB前端 JavaScript 学习笔记
  • 如何使用Python实现LRU缓存
  • OSCP渗透实战(第二期):Linux系统攻防与权限提升完全指南
  • LeetCode 118. 杨辉三角
  • AI原生应用架构白皮书 - AI原生应用架构及其关键要素
  • 蓝绿发布与金丝雀发布策略简介_笔记
  • 流媒体视频技术在明厨亮灶场景中的深度应用
  • 建设网站是否需要一个主机全球邮企业邮箱登录
  • 余弦相似度凭什么成了文本推荐的“方向指南针”?从几何夹角到语义匹配的AI密码
  • 解决Docker Hub被封的问题
  • 基于android的中天模拟键盘APP的设计与实现(初稿)
  • 数据结构——十七、线索二叉树找前驱与后继
  • 数据结构-----栈队列
  • 兰州网站制作有哪些怎样下载字体到wordpress
  • 【ASP.NET Core】分布式场景下ASP.NET Core中JWT应用教程
  • C++分布式语音识别服务实践——性能优化与实战部署