华为 鸿蒙HarmonyOS实战-ArkUI事件(焦点事件) - width(200)

📅 2026-11-30 👤 拼多多助力营销技巧 📂 pdd助力 👁️ 185 次阅读

先自我介绍一下,小编浙江大学毕业,去过华为、字节跳动等大厂,目前阿里P7

深知大多数程序员,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年最新HarmonyOS鸿蒙全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友。

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上鸿蒙开发知识点,真正体系化!

由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新

如果你需要这些资料,可以添加V获取:vip204888 (备注鸿蒙)

正文

.fontSize(40) .type(ButtonType.Normal) .borderRadius(20) .backgroundColor(‘#dadbd9’) }

Row({ space: 20 }) { Button(‘4’) .width(100) .height(100) .fontSize(40) .type(ButtonType.Normal) .borderRadius(20) .backgroundColor(‘#dadbd9’) Button(‘5’) .width(100) .height(100) .fontSize(40) .type(ButtonType.Normal) .borderRadius(20) .backgroundColor(‘#dadbd9’) }

Row({ space: 20 }) { Button(‘6’) .width(100) .height(100) .fontSize(40) .type(ButtonType.Normal) .borderRadius(20) .backgroundColor(‘#dadbd9’) Button(‘7’) .width(100) .height(100) .fontSize(40) .type(ButtonType.Normal) .borderRadius(20) .backgroundColor(‘#dadbd9’) } } } .width(480) .height(380) .justifyContent(FlexAlign.Center) .borderWidth(2) .borderColor(Color.Gray) .backgroundColor(Color.White) }, item => item) } .cachedCount(2) .index(0) .interval(4000) .indicator(true) .loop(true) .duration(1000) .itemSpace(0) .curve(Curve.Linear) .onChange((index: number) => { console.info(index.toString()); }) .margin({ left: 20, top: 20, right: 20 })

Row({ space: 40 }) { Button(‘←’) .fontSize(40) .fontWeight(FontWeight.Bold) .fontColor(Color.Black) .backgroundColor(Color.Transparent) .onClick(() => { this.swiperController.showPrevious(); }) Button(‘→’) .fontSize(40) .fontWeight(FontWeight.Bold) .fontColor(Color.Black) .backgroundColor(Color.Transparent) .onClick(() => { this.swiperController.showNext(); }) } .width(480) .height(50) .justifyContent(FlexAlign.Center) .borderWidth(2) .borderColor(Color.Gray) .backgroundColor(‘#f7f6dc’)

Row({ space: 40 }) { Button(‘Cancel’) .fontSize(30) .fontColor(‘#787878’) .type(ButtonType.Normal) .width(140) .height(50) .backgroundColor(‘#dadbd9’)

Button(‘OK’) .fontSize(30) .fontColor(‘#787878’) .type(ButtonType.Normal) .width(140) .height(50) .backgroundColor(‘#dadbd9’) .onClick(() => { promptAction.showToast({ message: ‘Button OK on clicked’ }); }) } .width(480) .height(80) .justifyContent(FlexAlign.Center) .borderWidth(2) .borderColor(Color.Gray) .backgroundColor(‘#dff2e4’) .margin({ left: 20, bottom: 20, right: 20 }) }.backgroundColor(‘#f2f2f2’) .margin({ left: 50, top: 50, right: 20 }) } }

6.自定义TAB键走焦顺序

Button(‘1’).width(200).height(200) .fontSize(40) .backgroundColor(‘#dadbd9’) .tabIndex(1) // Button-1设置为第一个tabIndex节点 Button(‘←’) .fontSize(40) .fontWeight(FontWeight.Bold) .fontColor(Color.Black) .backgroundColor(Color.Transparent) .onClick(() => { this.swiperController.showPrevious(); }) .tabIndex(2) // Button-左箭头设置为第二个tabIndex节点 Button(‘OK’) .fontSize(30) .fontColor(‘#787878’) .type(ButtonType.Normal) .width(140).height(50).backgroundColor(‘#dadbd9’) .onClick(() => { promptAction.showToast({ message: ‘Button OK on clicked’ }); }) .tabIndex(3) // Button-OK设置为第三个tabIndex节点

曆6.1 groupDefaultFocus

我们分别将某个组件设置为tabIndex节点,设置完之后,只有当我们按下TAB/ShiftTab键在这3个组件上进行焦点切换时,才会出现快速走焦的效果。

为了解决这个问题,我们可以给每个区域的容器设置tabIndex属性。然而,这样设置存在一个问题:当首次走焦到容器上时,焦点会默认落在容器内的第一个可获焦组件上,而不是我们想要的Button1、左箭头、ButtonOK。

为了解决这个问题,我们引入了一个名为groupDefaultFocus的通用属性,该属性接受一个布尔值参数,默认值为false。使用该属性需要与tabIndex属性结合使用,首先使用tabIndex为每个区域(容器)定义焦点切换顺序,然后为Button1、左箭头、ButtonOK这些组件绑定groupDefaultFocus(true)。这样,在首次走焦到目标区域(容器)时,拥有groupDefaultFocus(true)绑定的子组件将同时获取焦点。

// xxx.ets import promptAction from ‘@ohos.promptAction’;

class MyDataSource implements IDataSource { private list: number[] = []; private listener: DataChangeListener;

constructor(list: number[]) { this.list = list; }

totalCount(): number { return this.list.length; }

getData(index: number): any { return this.list[index]; }

registerDataChangeListener(listener: DataChangeListener): void { this.listener = listener; }

unregisterDataChangeListener() { } }

@Entry @Component struct SwiperExample { private swiperController: SwiperController = new SwiperController() private data: MyDataSource = new MyDataSource([])

aboutToAppear(): void { let list = [] for (let i = 1; i <= 4; i++) { list.push(i.toString()); } this.data = new MyDataSource(list); }

build() { Column({ space: 5 }) { Swiper(this.swiperController) { LazyForEach(this.data, (item: string) => { Row({ space: 20 }) { // 设置该Row组件为tabIndex的第一个节点 Column() { Button(‘1’).width(200).height(200) .fontSize(40) .backgroundColor(‘#dadbd9’) .groupDefaultFocus(true) // 设置Button-1为第一个tabIndex的默认焦点 }

Column({ space: 20 }) { Row({ space: 20 }) { Button(‘2’) .width(100) .height(100) .fontSize(40) .type(ButtonType.Normal) .borderRadius(20) .backgroundColor(‘#dadbd9’) Button(‘3’) .width(100) .height(100) .fontSize(40) .type(ButtonType.Normal) .borderRadius(20) .backgroundColor(‘#dadbd9’) }

Row({ space: 20 }) { Button(‘4’) .width(100) .height(100) .fontSize(40) .type(ButtonType.Normal) .borderRadius(20) .backgroundColor(‘#dadbd9’) Button(‘5’) .width(100) .height(100) .fontSize(40) .type(ButtonType.Normal) .borderRadius(20) .backgroundColor(‘#dadbd9’) }

Row({ space: 20 }) { Button(‘6’) .width(100) .height(100) .fontSize(40) .type(ButtonType.Normal) .borderRadius(20) .backgroundColor(‘#dadbd9’) Button(‘7’) .width(100) .height(100) .fontSize(40) .type(ButtonType.Normal) .borderRadius(20) .backgroundColor(‘#dadbd9’) } } } .width(480) .height(380) .justifyContent(FlexAlign.Center) .borderWidth(2) .borderColor(Color.Gray) .backgroundColor(Color.White) .tabIndex(1) }, item => item) } .cachedCount(2) .index(0) .interval(4000) .indicator(true) .loop(true) .duration(1000) .itemSpace(0) .curve(Curve.Linear) .onChange((index: number) => { console.info(index.toString()); }) .margin({ left: 20, top: 20, right: 20 })

Row({ space: 40 }) { // 设置该Row组件为第二个tabIndex节点 Button(‘←’) .fontSize(40) .fontWeight(FontWeight.Bold) .fontColor(Color.Black) .backgroundColor(Color.Transparent) .onClick(() => { this.swiperController.showPrevious(); }) .groupDefaultFocus(true) // 设置Button-左箭头为第二个tabIndex节点的默认焦点 Button(‘→’) .fontSize(40) .fontWeight(FontWeight.Bold) .fontColor(Color.Black) .backgroundColor(Color.Transparent) .onClick(() => { this.swiperController.showNext(); }) } .width(480) .height(50) .justifyContent(FlexAlign.Center) .borderWidth(2) .borderColor(Color.Gray) .backgroundColor(‘#f7f6dc’) .tabIndex(2)

Row({ space: 40 }) { // 设置该Row组件为第三个tabIndex节点 Button(‘Cancel’) .fontSize(30) .fontColor(‘#787878’) .type(ButtonType.Normal) .width(140) .height(50) .backgroundColor(‘#dadbd9’)

Button(‘OK’) .fontSize(30) .fontColor(‘#787878’) .type(ButtonType.Normal) .width(140) .height(50) .backgroundColor(‘#dadbd9’) .defaultFocus(true) .onClick(() => { promptAction.showToast({ message: ‘Button OK on clicked’ }); }) .groupDefaultFocus(true) // 设置Button-OK为第三个tabIndex节点的默认焦点 } .width(480) .height(80) .justifyContent(FlexAlign.Center) .borderWidth(2) .borderColor(Color.Gray) .backgroundColor(‘#dff2e4’) .margin({ left: 20, bottom: 20, right: 20 }) .tabIndex(3) }.backgroundColor(‘#f2f2f2’) .margin({ left: 50, top: 50, right: 20 }) } }

曆6.2 focusOnTouch

接口:

focusOnTouch(value: boolean)

点击是指使用触屏或鼠标左键进行单击,默认为false的组件,例如Button,不绑定该API时,点击Button不会使其获焦,当给Button绑定focusOnTouch(true)时,点击Button会使Button立即获得焦点。

案例:

// requestFocus.ets import promptAction from ‘@ohos.promptAction’;

@Entry @Component struct RequestFocusExample { @State idList: string[] = [‘A’, ‘B’, ‘C’, ‘D’, ‘E’, ‘F’, ‘N’]

build() { Column({ space:20 }){ Button(“id: " + this.idList[0] + " focusOnTouch(true) + focusable(false)”) .width(400).height(70).fontColor(Color.White).focusOnTouch(true) .focusable(false) Button(“id: " + this.idList[1] + " default”) .width(400).height(70).fontColor(Color.White) Button(“id: " + this.idList[2] + " focusOnTouch(false)”) .width(400).height(70).fontColor(Color.White).focusOnTouch(false) Button(“id: " + this.idList[3] + " focusOnTouch(true)”) .width(400).height(70).fontColor(Color.White).focusOnTouch(true) }.width(‘100%’).margin({ top:20 }) } }

曆6.3 focusControl.requestFocus

在任意执行语句中调用该API,指定目标组件的id为方法参数,当程序执行到该语句时,会立即给指定的目标组件申请焦点。

接口:

focusControl.requestFocus(id: string)

案例:

// requestFocus.ets import promptAction from ‘@ohos.promptAction’; 拼多多帮助力入口

@Entry @Component struct RequestFocusExample { @State idList: string[] = [‘A’, ‘B’, ‘C’, ‘D’, ‘E’, ‘F’, ‘N’] @State requestId: number = 0

build() { Column({ space:20 }){ Row({space: 5}) { Button(“id: " + this.idList[0] + " focusable(false)”) .width(200).height(70).fontColor(Color.White) .id(this.idList[0]) .focusable(false) Button("id: " + this.idList[1]) .width(200).height(70).fontColor(Color.White) .id(this.idList[1]) } Row({space: 5}) { Button("id: " + this.idList[2]) .width(200).height(70).fontColor(Color.White) .id(this.idList[2]) Button("id: " + this.idList[3]) .width(200).height(70).fontColor(Color.White) .id(this.idList[3]) } Row({space: 5}) { Button("id: " + this.idList[4]) .width(200).height(70).fontColor(Color.White) .id(this.idList[4]) Button("id: " + this.idList[5]) .width(200).height(70).fontColor(Color.White) .id(this.idList[5]) } }.width(‘100%’).margin({ top:20 }) .onKeyEvent((e) => { if (e.keyCode >= 2017 && e.keyCode <= 2022) { this.requestId = e.keyCode - 2017; } else if (e.keyCode === 2030) { this.requestId = 6; } else { return; } if (e.type !== KeyType.Down) { return; } let res = focusControl.requestFocus(this.idList[this.requestId]); if (res) { promptAction.showToast({message: ‘Request success’}); } else { promptAction.showToast({message: ‘Request failed’}); } }) } }

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化的资料的朋友,可以添加V获取:vip204888 (备注鸿蒙)

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

ype !== KeyType.Down) { return; } let res = focusControl.requestFocus(this.idList[this.requestId]); if (res) { promptAction.showToast({message: ‘Request success’}); } else { promptAction.showToast({message: ‘Request failed’}); } }) } }

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化的资料的朋友,可以添加V获取:vip204888 (备注鸿蒙) [外链图片转存中…(img-YkviEvuk-1713213807146)]

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

相关链接

标签: 拼多多助力网站平台 赚钱项目 拼多多助力网 首码项目 手赚APP
拼多多助力营销技巧