【案例+】HarmonyOS官方模板优秀案例 (第期:金融理财 · 记账应用)

一、引言与背景在金融理财领域,记账应用是用户管理日常收支、规划预算的核心工具。HarmonyOS(鸿蒙系统)作为面向全场景的分布式操作系统,其官方模板为开发者提供了高效、规范的UI组件和业务逻辑框架。本期案例聚焦于“金融理财·记账应用”,我们将从基础概念开始,逐步深入,通过完整代码示例展示如何利用HarmonyOS的声明式UI(ArkTS)和状态管理机制,构建一个可运行的记账应用原型。## 二、基础概念:声明式UI与状态管理在HarmonyOS开发中,核心思想之一是声明式UI:开发者只需描述界面“应该是什么样子”,系统会自动处理UI的更新。状态管理则通过@State@Prop等装饰器实现数据驱动视图刷新。### 2.1 基础代码示例:一个简单的记账输入组件下面的代码展示了如何创建一个包含“金额输入”和“类别选择”的记账表单。它使用@State装饰器管理输入数据,当用户输入时,UI自动更新。typescript// 文件路径:pages/RecordEntry.etsimport router from '@ohos.router';@Entry@Componentstruct RecordEntry { @State amount: string = ''; // 金额输入,使用@State监听变化 @State category: string = '餐饮'; // 默认类别 build() { Column() { Text('记账录入') .fontSize(24) .fontWeight(FontWeight.Bold) .margin({ bottom: 20 }) // 金额输入框 TextInput({ placeholder: '请输入金额(元)', text: this.amount }) .onChange((value) => { this.amount = value; // 更新状态,触发UI刷新 }) .width('80%') .margin({ bottom: 10 }) // 类别选择下拉框(简化示例) Select([{ value: '餐饮' }, { value: '交通' }, { value: '购物' }]) .value(this.category) .onSelect((index) => { this.category = index === 0 ? '餐饮' : (index === 1 ? '交通' : '购物'); }) .width('80%') .margin({ bottom: 20 }) Button('保存记录') .onClick(() => { // 模拟保存逻辑(此处仅控制台输出) console.info(`保存记录:金额=${this.amount}, 类别=${this.category}`); }) .width('50%') .backgroundColor('#007AFF') .fontColor('#FFFFFF') } .width('100%') .height('100%') .padding(20) }}代码解析:- @State amount:当用户修改输入框内容时,amount值更新,UI自动重新渲染。- @State category:下拉选择改变时,类别同步更新。- 按钮点击时,通过console.info模拟保存,实际应用中可调用数据库API。## 三、进阶应用:数据持久化与列表展示基础记账功能需要将记录存储起来,并展示历史列表。HarmonyOS提供了@StorageLink@LocalStorage用于应用级数据共享,以及@Observed装饰器实现深层对象监听。### 3.1 进阶代码示例:带本地存储的记账列表以下示例使用@LocalStorage模拟本地存储,实现新增记录并显示在列表中。注意:实际开发中可结合@ohos.data.preferences或数据库。typescript// 文件路径:pages/BillList.etsimport router from '@ohos.router';import { ArrayList } from '@ohos.util.ArrayList';@Observedclass BillRecord { id: number; amount: number; category: string; date: string; constructor(id: number, amount: number, category: string, date: string) { this.id = id; this.amount = amount; this.category = category; this.date = date; }}@Entry@Componentstruct BillList { // 使用@LocalStorage来模拟持久化(实际应使用数据库) @LocalStorageProp('billRecords') records: BillRecord[] = []; build() { Column() { Text('历史账单') .fontSize(24) .fontWeight(FontWeight.Bold) .margin({ bottom: 20 }) // 添加新记录按钮(模拟) Button('添加示例记录') .onClick(() => { // 创建新记录并添加到数组 const newRecord = new BillRecord( this.records.length + 1, Math.floor(Math.random() * 1000) + 10, ['餐饮', '交通', '购物'][Math.floor(Math.random() * 3)], new Date().toLocaleDateString('zh-CN') ); this.records.push(newRecord); // 自动触发UI更新 }) .width('60%') .margin({ bottom: 20 }) // 使用List组件展示记录 List() { ForEach(this.records, (item: BillRecord) => { ListItem() { Row() { Text(item.category) .fontSize(16) .fontWeight(FontWeight.Medium) .width('30%') Text(`¥${item.amount.toFixed(2)}`) .fontSize(16) .fontColor('#FF6B35') .width('30%') Text(item.date) .fontSize(14) .fontColor('#888888') .width('40%') } .width('100%') .padding(12) .border({ width: 1, color: '#E0E0E0', style: BorderStyle.Solid }) .borderRadius(8) .margin({ bottom: 8 }) } }) } .width('100%') .layoutWeight(1) } .width('100%') .height('100%') .padding(20) }}代码解析:- @Observed class BillRecord:标记类为可观察,当对象属性变化时,UI会更新。- @LocalStorageProp:将数据绑定到本地存储,应用重启后数据会丢失(实际需持久化)。- ForEach循环渲染列表:当records数组变化时,列表自动增删。- 按钮点击添加随机记录,展示动态交互。## 四、高级应用:分布式数据同步与图表可视化HarmonyOS的分布式能力允许记账数据在手机、平板、手表等设备间同步。此外,利用@ohos.multimedia.media和画布能力可绘制简单的收支图表。以下简要说明核心思路:### 4.1 分布式数据同步(概念)使用@ohos.distributedKVStore(分布式键值数据库),可以跨设备同步记账数据。例如:typescript// 伪代码示例(需配置分布式权限)import distributedKVStore from '@ohos.distributedKVStore';const kvManager = distributedKVStore.createKVManager({ bundleName: 'com.example.bill' });const kvStore = await kvManager.getKVStore('billStore');await kvStore.put('2025-01-01', JSON.stringify({ amount: 100, category: '餐饮' }));### 4.2 图表可视化(Canvas)利用Canvas组件绘制月度收支柱状图:typescript// 简化版Canvas绘图Canvas(this.context) .onReady(() => { const ctx = this.context; ctx.fillStyle = '#4CAF50'; ctx.fillRect(0, 0, 50, 200); // 收入柱 ctx.fillStyle = '#FF5722'; ctx.fillRect(60, 100, 50, 100); // 支出柱 }) .width(200) .height(300);## 五、总结通过本案例,我们从基础概念出发,逐步深入HarmonyOS记账应用的开发:1. 基础篇:掌握声明式UI和@State状态管理,构建输入表单。2. 进阶篇:利用@Observed@LocalStorage实现数据持久化与列表展示。3. 高级篇:了解分布式同步和Canvas图表绘制,为全场景金融理财应用奠定基础。HarmonyOS官方模板不仅提供了高效的开发范式,更通过分布式能力、组件化设计,帮助开发者快速构建跨设备的金融理财工具。建议读者结合官方文档,尝试将记账应用扩展到多端同步、预算提醒等高级功能。

Logo

讨论HarmonyOS开发技术,专注于API与组件、DevEco Studio、测试、元服务和应用上架分发等。

更多推荐