新闻详情

新闻详情

首页 / 资讯中心 / 详情

OpenHarmony 进阶 UI 组件、自定义组件实战与交互开发

发布时间:2026/10/29 21:52:22来源:尧图网络
OpenHarmony 进阶 UI 组件、自定义组件实战与交互开发
一、引言在上一篇文章中我们学习了 OpenHarmony ArkUI 基础组件与基础布局能够完成常规页面搭建。在实际项目开发中还会用到选择器、弹窗、进度条、开关等进阶交互组件同时页面中大量重复的 UI 模块需要通过自定义组件实现代码复用、降低维护成本。本文聚焦进阶交互组件、常用拓展组件、自定义组件封装三大板块结合落地案例讲解用法、属性与开发规范帮助开发者实现更丰富的界面交互与工程化开发。二、常用进阶交互组件详解及案例2.1 Toggle 开关组件多用于设置页面、功能启停蓝牙、消息通知、夜间模式等支持状态监听与样式自定义。核心属性isOn开关状态、onChange状态切换回调、selectedColor开启颜色Entry Component struct ToggleDemo { // 绑定开关状态 State isOpen: boolean false build() { Column({ space: 20 }) { Row() { Text(开启消息通知).fontSize(18).layoutWeight(1) Toggle({ isOn: this.isOpen }) .selectedColor(#007DFF) .onChange((isOn: boolean) { this.isOpen isOn console.log(开关状态, isOn ? 已开启 : 已关闭) }) } .width(100%) .padding(15) .backgroundColor(#F5F5F5) .borderRadius(8) } .padding(20) } }2.2 Slider 滑动条组件适用于音量调节、亮度调节、进度选择等场景支持滑动取值、样式定制。核心属性min/max取值范围、value当前值、onChange滑动回调Entry Component struct SliderDemo { State currentValue: number 50 build() { Column({ space: 15 }) { Text(当前数值${this.currentValue}).fontSize(18) Slider({ min: 0, max: 100, value: this.currentValue }) .trackColor(#E5E5E5) .selectedColor(#2ECC71) .blockColor(#FFFFFF) .onChange((value: number) { this.currentValue value }) } .padding(20) } }2.3 Progress 进度条组件展示任务加载、文件下载、页面加载进度分为线性进度条、环形进度条。核心属性value当前进度、total总进度、type进度条类型Entry Component struct ProgressDemo { State progress: number 60 build() { Column({ space: 30 }) { Text(线性进度条).fontSize(18) Progress({ value: this.progress, total: 100 }) .width(100%) .color(#007DFF) Text(环形进度条).fontSize(18) Progress({ value: this.progress, total: 100, type: ProgressType.Ring }) .width(100) .height(100) .color(#FF6734) } .padding(20) } }2.4 Select 下拉选择器替代传统下拉菜单用于选项选择、分类筛选、地区选择等场景。核心属性options选项数组、selected默认选中项、onSelect选择回调Entry Component struct SelectDemo { State selectText: string 请选择分类 // 下拉选项 private options: ResourceStr[] [数码产品, 生活用品, 服饰穿搭, 美食生鲜] build() { Column({ space: 20 }) { Select(this.options) .width(200) .height(40) .onSelect((index: number, text: string) { this.selectText text }) Text(已选择${this.selectText}).fontSize(18) } .padding(20) } }2.5 AlertDialog 弹窗组件全局弹窗用于消息提示、二次确认、操作提醒是应用高频交互组件。使用说明通过show()方法唤起弹窗支持标题、内容、多按钮配置。Entry Component struct DialogDemo { build() { Column() { Button(点击弹出确认弹窗) .width(200) .onClick(() { AlertDialog.show({ title: 温馨提示, message: 确定要执行该操作吗, primaryButton: { value: 确定, action: () console.log(点击确定) }, secondaryButton: { value: 取消, action: () console.log(点击取消) } }) }) } .width(100%) .height(100%) .justifyContent(FlexAlign.Center) } }三、自定义组件开发实战ArkUI 支持将重复使用的 UI 模块封装为自定义组件遵循Component装饰器定义可传入参数、绑定状态大幅简化代码。3.1 基础自定义组件纯展示卡片封装通用信息卡片可在列表、首页、个人中心重复调用。// 自定义卡片组件 Component struct CardItem { // 外部传入参数 Prop title: string Prop desc: string build() { Column({ space: 8 }) { Text(this.title) .fontSize(20) .fontWeight(FontWeight.Bold) Text(this.desc) .fontSize(16) .fontColor(#666666) } .width(100%) .padding(15) .backgroundColor(#FFFFFF) .borderRadius(10) .shadow({ radius: 5, color: #E0E0E0 }) } } // 页面入口调用自定义组件 Entry Component struct CustomCardPage { build() { Column({ space: 15 }) { // 多次复用自定义卡片 CardItem({ title: 功能一, desc: OpenHarmony 分布式能力介绍 }) CardItem({ title: 功能二, desc: ArkUI 组件实战开发教程 }) CardItem({ title: 功能三, desc: 应用性能优化技巧分享 }) } .width(100%) .padding(20) .backgroundColor(#F5F5F5) } }3.2 带交互的自定义组件自定义按钮封装带点击事件的交互组件支持样式、文字、事件外部传参。// 自定义交互按钮组件 Component struct MyButton { Prop btnText: string Prop btnColor: ResourceColor // 点击事件回调 BuilderParam clickFunc: () void build() { Button(this.btnText) .width(160) .height(45) .backgroundColor(this.btnColor) .borderRadius(22) .onClick(this.clickFunc) } } Entry Component struct CustomBtnPage { build() { Column({ space: 20 }) { MyButton({ btnText: 提交表单, btnColor: #007DFF, clickFunc: () console.log(点击提交按钮) }) MyButton({ btnText: 重置内容, btnColor: #999999, clickFunc: () console.log(点击重置按钮) }) } .width(100%) .height(100%) .justifyContent(FlexAlign.Center) } }四、综合实战设置页面组件组合 自定义组件整合上文所有进阶组件与自定义组件搭建一套完整的应用设置页面可直接用于项目开发。// 自定义设置项组件 Component struct SettingItem { Prop title: string BuilderParam content: () void build() { Row() { Text(this.title) .fontSize(18) .layoutWeight(1) this.content() } .width(100%) .height(60) .padding({ left: 15, right: 15 }) .backgroundColor(#FFFFFF) } } Entry Component struct SettingPage { State noticeOpen: boolean true State brightness: number 70 build() { Column() { // 页面标题 Text(应用设置) .fontSize(24) .fontWeight(FontWeight.Bold) .margin({ bottom: 20 }) // 复用自定义设置项 开关 SettingItem({ title: 消息通知 }) { Toggle({ isOn: this.noticeOpen }) .onChange((val) this.noticeOpen val) } // 复用自定义设置项 滑动条 SettingItem({ title: 屏幕亮度 }) { Slider({ min: 0, max: 100, value: this.brightness }) .width(120) .onChange((val) this.brightness val) } // 下拉选择 SettingItem({ title: 默认分类 }) { Select([推荐, 热门, 最新]) .width(100) } Blank().layoutWeight(1) // 底部退出按钮 Button(退出登录) .width(100%) .height(50) .backgroundColor(#FF4444) .margin({ top: 30 }) .onClick(() { AlertDialog.show({ title: 退出提醒, message: 确定要退出当前账号吗, primaryButton: { value: 确定 }, secondaryButton: { value: 取消 } }) }) } .width(100%) .height(100%) .padding(20) .backgroundColor(#F5F5F5) } }五、开发总结与最佳实践5.1 进阶组件使用要点弹窗AlertDialog属于全局弹窗避免在循环内频繁调用防止层级错乱滑动条、开关、选择器必须绑定状态变量保证 UI 与数据双向同步进度条优先用于异步任务场景提升用户体验。5.2 自定义组件开发规范通用 UI 模块卡片、列表项、按钮、设置栏优先封装为自定义组件组件传参使用Prop交互事件使用BuilderParam保证组件解耦单一自定义组件只负责单一功能遵循单一职责原则。5.3 性能优化建议大量重复组件使用ForEach遍历渲染减少重复代码非高频更新的状态合理划分组件层级减少全局刷新统一组件样式圆角、间距、颜色维护全局风格。六、拓展学习方向封装全局公共组件库统一项目 UI 风格结合动画组件为开关、弹窗添加过渡动效学习CustomDialog自定义弹窗实现个性化弹窗样式。
网站建设高端定制企业官网
RELATED

相关资讯

更多精彩内容,欢迎继续阅读

较早相关资讯

最新相关资讯

【外设】之大彩串口显示屏 2026/10/28 16:06:12

【外设】之大彩串口显示屏

大彩串口屏初步使用 1 .官网下载 STM32 屏幕 GUI 设计资料 http://www.gz-dc.com/category/typeid/4112 找到 STM32 Keil 工程,移植相关代码因项目而异进行移植,由于项目简单,本人只对用到的指令接口进行修改。 比如:注意事项&…

阅读更多 →
无法下载Windows系统iso文件 2026/10/29 23:30:48

无法下载Windows系统iso文件

当我遇到这个问题的时候,我打开了一个网站: 登录 然后我打算下载的时候: 突然那个官方的连接就可以下载了:

阅读更多 →
【清华代码熊】DeepSeek V4.1 Flash 后训练详解 2026/10/29 13:36:24

【清华代码熊】DeepSeek V4.1 Flash 后训练详解

📌 上期解析了 DeepSeek V4.1 Flash 模型架构改进,本期解析 DeepSeek V4.1 Flash 预训练/后训练技术: 🌟 预训练:45T 文本 多模态混合语料、直接训练 sparse attention(取消 DeepSeek V4 的 dense 冷启动&…

阅读更多 →
Shuffle-R1: Efficient RL framework for Multimodal Large Language Models via Data-centric Dynamic ... 2026/10/29 19:55:35

Shuffle-R1: Efficient RL framework for Multimodal Large Language Models via Data-centric Dynamic ...

文章主要内容和创新点 主要内容 本文聚焦于多模态大语言模型(MLLM)强化学习(RL)训练中的效率问题,提出了一个名为Shuffle-R1的框架。研究发现,当前RL训练存在两个关键缺陷: 优势值坍缩(Advantage Collapsing):批次中大多数优势值集中在零附近,导致有效梯度信号被淹…

阅读更多 →
PRvL: Quantifying the Capabilities and Risks of Large Language Models for PII Redaction 2026/10/29 13:36:50

PRvL: Quantifying the Capabilities and Risks of Large Language Models for PII Redaction

一、文章主要内容总结 本文聚焦于利用大型语言模型(LLMs)实现个人身份信息(PII)脱敏的研究,旨在解决传统脱敏方法(如基于规则的系统、领域特定命名实体识别(NER)模型)泛化能力差、跨格式/跨语境适应性弱的问题。 研究通过全面评估多种LLM架构(包括密集型LLM(D-LLM…

阅读更多 →
LLaVA-RE: Binary Image-Text Relevancy Evaluation with Multimodal Large Language Model 2026/10/28 16:06:10

LLaVA-RE: Binary Image-Text Relevancy Evaluation with Multimodal Large Language Model

文章主要内容和创新点 主要内容 本文聚焦于二进制图像-文本相关性评估任务(判断图像与文本“相关”或“不相关”),针对该任务中文本格式多样、相关性定义随场景变化等挑战,提出了基于多模态大语言模型(MLLM)的解决方案LLaVA-RE。 模型设计:LLaVA-RE基于LLaVA 1.5架构,…

阅读更多 →

今日资讯

本周资讯

本月资讯

看完文章仍有疑问?

联系尧图顾问,获取一对一建站咨询

立即免费咨询 📞 400-888-8888
📞 ✉