HarmonyOS5.0,中HarmonyOS Design 系统的完整 UI 实例!
摘要:本文展示了一个基于ArkTS和HarmonyOS Design的待办事项应用完整实现。应用采用Flex布局和响应式设计,包含任务添加、状态切换和滑动删除功能,遵循鸿蒙设计规范(主色#007AFF,16px间距等)。核心特性包括:@State数据绑定、手势操作、Toast反馈和默认动效。代码结构清晰,包含任务列表管理、UI构建和交互逻辑,支持在DevEco Studio中直接运行。文章还提供了
·
以下是一个基于 ArkTS 和 HarmonyOS Design 系统的完整 UI 实例,实现一个「待办事项」应用,包含数据绑定、交互逻辑和设计规范应用:
// entry.ets
@Entry
@Component
struct TodoList {
// 状态管理
@State private tasks: Array<{ id: number, text: string, completed: boolean }> = []
@State private newTask: string = ""
private nextId: number = 1
// 构建UI
build() {
Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center }) {
// 标题栏
Text('鸿蒙待办')
.fontWeight(FontWeight.Bold)
.fontSize(32)
.margin({ top: 20 })
// 输入区域
Flex({ justifyContent: FlexAlign.SpaceBetween, alignItems: ItemAlign.Center }) {
TextInput(this.newTask)
.placeholder('输入新任务')
.width(300)
.height(40)
.borderRadius(8)
.onChange((value: string) => this.newTask = value)
Button('添加')
.onClick(() => this.addTask())
.type(ButtonType.Capsule)
.width(80)
}
.padding({ left: 20, right: 20 })
// 任务列表
List({ space: 10 }) {
ForEach(this.tasks, (task) => {
ListItem()
.height(60)
.padding({ left: 16 })
.onClick(() => this.toggleTask(task.id))
.onSwipe((direction: SwipeDirection) => {
if (direction === SwipeDirection.Right) {
this.deleteTask(task.id)
}
}) {
Flex({ justifyContent: FlexAlign.SpaceBetween }) {
Text(task.text)
.fontColor(task.completed ? '#999' : '#000')
.lineHeight(20)
.fontStyle(task.completed ? FontStyle.Italic : FontStyle.Normal)
Checkbox()
.checked(task.completed)
.onChange((checked) => this.toggleTask(task.id))
}
.width('100%')
}
}, (task: { id: number }) => task.id.toString())
}
.margin({ top: 20 })
// 状态提示
if (this.tasks.length === 0) {
Text('暂无待办事项')
.fontSize(16)
.color('#666')
.textAlign(TextAlign.Center)
.margin({ top: 50 })
}
}
.width('100%')
.height('100%')
}
// 添加任务
private addTask() {
if (this.newTask.trim()) {
this.tasks = [...this.tasks, {
id: this.nextId++,
text: this.newTask.trim(),
completed: false
}]
this.newTask = ""
}
}
// 切换任务状态
private toggleTask(id: number) {
this.tasks = this.tasks.map(task =>
task.id === id ? {...task, completed: !task.completed} : task
)
}
// 删除任务
private deleteTask(id: number) {
this.tasks = this.tasks.filter(task => task.id !== id)
context.showToast({
message: '任务已删除',
icon: 'success'
})
}
}
// config.json
{
"app": {
"bundleName": "com.example.todo",
"version": {
"code": 1,
"name": "1.0.0"
},
"reqPermissions": [
"ohos.permission.INTERNET"
]
},
"deviceConfig": {
"default": {
"designWidth": "1080",
"designHeight": "1920"
}
},
"module": {
"abilities": [
{
"name": ".TodoListAbility",
"icon": "$media:icon",
"label": "$string:app_name",
"launchType": "standard"
}
]
}
}
设计规范应用说明:
- 布局系统:使用 Flex 布局实现响应式设计,符合 HarmonyOS 的「流动布局」理念
- 色彩规范:
- 主色:#007AFF(按钮/交互元素)
- 文本色:#000(未完成)/#999(已完成)
- 背景色:#FFFFFF(符合鸿蒙浅色主题)
- 组件规范:
- 使用官方推荐的 Capsule 按钮样式
- 复选框与列表项对齐
- 16px 基础间距单位
- 交互反馈:
- 滑动删除操作
- Toast 状态提示
- 文本输入自动高度
- 动效规范:
- 列表项点击涟漪效果(默认集成)
- 状态变更过渡动画
实现特性:
- 数据驱动视图(@State 自动更新)
- 手势操作支持(滑动删除)
- 响应式布局(自适应不同屏幕尺寸)
- 完整的状态管理闭环
- 符合 ArkTS 最佳实践
运行准备:
- 在 DevEco Studio 创建新项目时选择「Empty Ability」模板
- 替换默认代码为上述内容
- 配置设备/模拟器(API Level 9+)
扩展建议:
- 添加本地存储:
import storage from '@ohos.storage'
// 在类中添加
private async saveData() {
try {
await storage.write({
key: 'todoList',
value: JSON.stringify(this.tasks)
})
} catch (err) {
console.error('Save failed:', err)
}
}
private async loadData() {
try {
const data = await storage.read({
key: 'todoList'
})
if (data.data) {
this.tasks = JSON.parse(data.data)
}
} catch (err) {
console.error('Load failed:', err)
}
}
- 添加 Material Design 动效:
import animation from '@ohos.animation'
// 在点击事件中添加
private async animateItem(item: ListItem) {
const animationObj = item.animateTo({
scale: [1, 0.9, 1],
opacity: [1, 0.5, 1]
}, {
duration: 300,
easing: 'ease-in-out'
})
}
该实例完整展示了 HarmonyOS Design 的核心要素,可直接在 DevEco Studio 中运行。如需添加更多功能(如分类标签、数据同步等),可基于此框架扩展。
更多推荐
所有评论(0)