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

本篇内容:ArkTS开发系列之事件(2.8.1触屏、键鼠、焦点事件)

上篇回顾: ArkTS开发系列之导航 (2.7动画)

本篇内容:ArkTS开发系列之事件(2.8.1触屏、键鼠、焦点事件)

一、知识储备

1. 触屏事件:包括点击事件、拖拽事件、触摸事件。

  • 点击事件
Button()....onClick(()=>{})
  • 拖拽事件:是指手指/手写笔长按组件(>=500ms),并拖拽到接收区域释放的事件
 .onDragEnter((event, params) => {console.error('onDragEnter event: ' + JSON.stringify(event))console.error('onDragEnter params: ' + JSON.stringify(params))}).onDragLeave((event, params) => {console.error('onDragLeave event: ' + JSON.stringify(event))console.error('onDragLeave params: ' + JSON.stringify(params))}).onDragMove((event, params) => {console.error('onDragMove event: ' + JSON.stringify(event))console.error('onDragMove params: ' + JSON.stringify(params))}).onDrop((event: DragEvent, params: string) => {console.error('onDrop event: ' + JSON.stringify(event))console.error('onDrop params: ' + JSON.stringify(params))this.color = Color.Yellow;})
  • 触摸事件:包括按下(down)、滑动(move)和抬起(up)
  .onTouch((event: TouchEvent)=>{if (event.type == TouchType.Down) {console.error("down: " + event.touches[0].x + " " + event.touches[0].y)console.error("down: " + event.touches[0].screenX + " " + event.touches[0].screenY)}if (event.type == TouchType.Move) {console.error("Move: " + event.touches[0].x + " " + event.touches[0].y)console.error("Move: " + event.touches[0].screenX + " " + event.touches[0].screenY)}if (event.type == TouchType.Up) {console.error("Up: " + event.touches[0].x + " " + event.touches[0].y)console.error("Up: " + event.touches[0].screenX + " " + event.touches[0].screenY)}})

2. 键鼠事件&按键事件

  • 键鼠事件
.onHover((event: boolean) => {if (event) {this.msg = '鼠标在玩具之上'} else {this.msg = '鼠标离开了玩具'}console.error(this.msg)})
.onMouse((event: MouseEvent)=>{this.msg = '鼠标点击了我'console.error(this.msg)console.error(JSON.stringify(event))})
.hoverEffect(HoverEffect.Scale)
  • 按键事件
   .onKeyEvent((event: KeyEvent)=>{if (event.type == KeyType.Down) {}if (event.type == KeyType.Up) {}this.msg = event.keyText})

3. 焦点

  • 获得焦点回调
       .onFocus(() => {this.focusMsg = '获取焦点'})
  • 失去焦点回调
       .onBlur(() => {this.focusMsg = '失去焦点'})
  • 设置组件是否可以获得焦点
    • button、checkBox、textInput此类组件 默认可获得焦点
    • text 、image此类组件默认无法获得组件,可设置focusable为true来获得焦点能力
    • blank、circle此类组件则无论如何设置都无法具有获取焦点的能力
      .focusable(true)//
  • 自定义某个组件默认获得焦点
.defaultFocus(true)
  • 点击获取焦点
.focusOnTouch(true)

二、效果一览

三、源码剖析

http://www.lryc.cn/news/382280.html

相关文章:

  • 测试的基础知识大全【测试概念、分类、模型、流程、测试用例书写、用例设计、Bug、基础功能测试实战】
  • Power Apps
  • qt图像处理-将OpenCV的cv::Mat类型转换为QImage类型
  • 代码随想录训练营第十八天 530二叉搜索树的最小绝对差 501二叉搜索树中的众数 236二叉树的最近公共祖先
  • 微信小程序之横向列表展示
  • 无人机巡检小羊仿真
  • springboot redission 分布式锁
  • Vuex中的重要核心属性
  • Redis哨兵集群搭建
  • 网络爬虫requests库使用指南
  • VSCODE 配置C++ 与OPENCV
  • C语言小例程28/100
  • WPF文本绑定显示格式StringFormat设置-特殊格式时间日期和多数据绑定
  • Java包介绍
  • 【2024.6.21】今日科技时事:科技前沿大事件
  • LeetCode:经典题之1491、896 题解与延伸
  • 2024三掌柜赠书活动第二十五期:Rust 游戏开发实战
  • 基于Java蛋糕甜品商城系统设计和实现(源码+LW+调试文档+讲解等)
  • Tomcat get请求传数组集合参数
  • 信息学奥赛初赛天天练-34-CSP-J2021完善程序-按位异或、模拟算法、数组模拟环、约瑟夫问题应用
  • 【计算机视觉】人脸算法之图像处理基础知识(六)
  • 仓颉编程语言入门
  • 在前端项目中,如何处理错误和异常?
  • Ubuntu系统下修改网卡IP地址
  • Scrapy如何对爬虫数据进行清洗和处理?
  • Linux:基础IO(三.软硬链接、动态库和静态库、动精态库的制作和加载)
  • 低价可转债崩盘,发生了什么?
  • 【面试题】马上金九银十了,简历该准备起来了,面试题你准备好了吗 ?浅谈 JS 浅拷贝和深拷贝
  • 最新OPPO 真我手机 一加手机 使用adb命令永久关闭系统更新教程
  • OnlyOffice:现代办公的最佳选择