
回复
大家好!今天想和大家分享一个特别有意思的HarmonyOS项目——五行知识分享应用。这个demo不仅界面简洁美观,更重要的是它集成了系统分享功能,可以让我们把有趣的五行知识一键分享给朋友!
作为一个对传统文化感兴趣的开发者,我觉得把古老的五行哲学用现代技术呈现出来特别有意义。这个项目让我学到了:
想象一下:打开应用,看到一个漂亮的五行知识卡片,点击"分享知识"按钮,就能把当前的知识点分享到微信、QQ等社交平台!
import { uniformTypeDescriptor as utd } from '@kit.ArkData';
import common from '@ohos.app.ability.common';
import promptAction from '@ohos.promptAction';
import { systemShare } from '@kit.ShareKit';
小白理解:
@kit.ArkData
:提供数据类型定义@ohos.app.ability.common
:应用上下文相关@ohos.promptAction
:提示消息功能@kit.ShareKit
:系统分享功能的核心模块
@Entry
@Component
struct Index {
@State currentKnowledge: string = '五行是中国古代哲学的重要概念...';
@State knowledgeIndex: number = 0;
// 五行知识库
private knowledgeList: string[] = [
'五行是中国古代哲学的重要概念...',
'金代表收敛、变革,对应秋季...',
'木代表生长、发展,对应春季...',
// ... 更多知识点
];
}
知识点解析:
@State
:管理当前显示的知识点和索引knowledgeList
:存储所有的五行知识点这是整个demo最酷的部分!我们来实现系统分享功能:
private async share(title: string, content: string, description: string) {
try {
// 1. 创建分享数据对象
let shareData: systemShare.SharedData = new systemShare.SharedData({
utd: utd.UniformDataType.TEXT, // 指定分享文本类型
content: content, // 分享的主要内容
title: title, // 分享标题
description: description, // 分享描述
});
// 2. 创建分享控制器
let controller: systemShare.ShareController = new systemShare.ShareController(shareData);
// 3. 获取应用上下文
const uiContext: UIContext = this.getUIContext();
const context: common.UIAbilityContext = uiContext.getHostContext() as common.UIAbilityContext;
// 4. 显示分享界面
await controller.show(context, {
selectionMode: systemShare.SelectionMode.SINGLE, // 单选模式
previewMode: systemShare.SharePreviewMode.DEFAULT, // 默认预览
});
console.log('分享成功');
} catch (error) {
console.error('分享失败:', error);
promptAction.showToast({
message: '分享失败,请重试',
duration: 2000
});
}
}
分享功能详解:
// 分享五行知识
async shareKnowledge() {
const title = '五行知识分享';
const description = '分享有趣的五行哲学知识';
await this.share(title, this.currentKnowledge, description);
}
设计思路:
shareKnowledge()
就能分享
// 切换下一个知识点
nextKnowledge() {
this.knowledgeIndex = (this.knowledgeIndex + 1) % this.knowledgeList.length;
this.currentKnowledge = this.knowledgeList[this.knowledgeIndex];
}
算法技巧:
%
实现循环切换
build() {
Column() {
// 标题
Text('五行知识分享')
.fontSize(24)
.fontWeight(FontWeight.Bold)
// 知识卡片
Column() {
Text(this.currentKnowledge)
.fontSize(18)
.textAlign(TextAlign.Center)
.padding(20)
.backgroundColor('#f5f5f5')
.borderRadius(12)
}
.width('100%')
.alignItems(HorizontalAlign.Center)
// 按钮区域
Row() {
Button('分享知识')
.backgroundColor('#007DFF')
.onClick(() => { this.shareKnowledge(); })
Button('下一个')
.backgroundColor('#34C759')
.margin({ left: 20 })
.onClick(() => { this.nextKnowledge(); })
}
.margin({ top: 30 })
.justifyContent(FlexAlign.Center)
}
.width('100%')
.height('100%')
.padding(20)
}
界面设计要点:
import { uniformTypeDescriptor as utd } from '@kit.ArkData';
import common from '@ohos.app.ability.common';
import promptAction from '@ohos.promptAction';
import { systemShare } from '@kit.ShareKit';
@Entry
@Component
struct Index {
@State currentKnowledge: string = '五行是中国古代哲学的重要概念...';
@State knowledgeIndex: number = 0;
private knowledgeList: string[] = [/* 五行知识点数组 */];
// 系统分享功能
private async share(title: string, content: string, description: string) {
try {
let shareData = new systemShare.SharedData({
utd: utd.UniformDataType.TEXT,
content: content,
title: title,
description: description,
});
let controller = new systemShare.ShareController(shareData);
const uiContext = this.getUIContext();
const context = uiContext.getHostContext() as common.UIAbilityContext;
await controller.show(context, {
selectionMode: systemShare.SelectionMode.SINGLE,
previewMode: systemShare.SharePreviewMode.DEFAULT,
});
} catch (error) {
promptAction.showToast({ message: '分享失败,请重试' });
}
}
// 分享当前知识点
async shareKnowledge() {
await this.share('五行知识分享', this.currentKnowledge, '分享有趣的五行哲学知识');
}
// 切换知识点
nextKnowledge() {
this.knowledgeIndex = (this.knowledgeIndex + 1) % this.knowledgeList.length;
this.currentKnowledge = this.knowledgeList[this.knowledgeIndex];
}
build() {
Column() {
Text('五行知识分享').fontSize(24).fontWeight(FontWeight.Bold)
Column() {
Text(this.currentKnowledge)
.fontSize(18)
.padding(20)
.backgroundColor('#f5f5f5')
.borderRadius(12)
}
.width('100%').alignItems(HorizontalAlign.Center)
Row() {
Button('分享知识').onClick(() => { this.shareKnowledge(); })
Button('下一个').onClick(() => { this.nextKnowledge(); })
}
.margin({ top: 30 }).justifyContent(FlexAlign.Center)
}
.width('100%').height('100%').padding(20)
}
}
这里是在模拟器,所以只能演示添加到中转站,如果是在真机上,可以分享到各种APP中,直接进行使用。
问题:刚开始调用分享功能时总是失败
解决:需要在module.json5
文件中添加权限声明。
{
"module": {
"requestPermissions": [
{
"name": "ohos.permission.SYSTEM_SHARE"
}
]
}
}
问题:分享按钮点击后没有立即响应
解决:分享函数是异步的,需要使用await
关键字
// 错误写法
onClick(() => {
this.shareKnowledge(); // 不会等待分享完成
})
// 正确写法
onClick(async () => {
await this.shareKnowledge(); // 等待分享完成
})
问题:获取应用上下文时类型转换错误 解决:需要使用正确的类型断言
// 错误写法
const context = uiContext.getHostContext();
// 正确写法
const context = uiContext.getHostContext() as common.UIAbilityContext;
为了让应用更有价值,我精心设计了五行知识点:
private knowledgeList: string[] = [
'五行是中国古代哲学的重要概念,包括金、木、水、火、土五种基本元素。',
'金代表收敛、变革,对应秋季,象征收获和变革的力量。',
'木代表生长、发展,对应春季,象征生命和成长的活力。',
'水代表流动、智慧,对应冬季,象征变化和深沉的智慧。',
'火代表热情、光明,对应夏季,象征热情和光明的力量。',
'土代表稳定、包容,对应四季交替,象征稳定和包容的特性。',
'五行相生:木生火、火生土、土生金、金生水、水生木。',
'五行相克:木克土、土克水、水克火、火克金、金克木。'
];
内容设计原则:
@State favoriteKnowledge: string[] = [];
// 收藏当前知识点
addToFavorites() {
if (!this.favoriteKnowledge.includes(this.currentKnowledge)) {
this.favoriteKnowledge.push(this.currentKnowledge);
}
}
@State searchKeyword: string = '';
// 搜索五行知识
searchKnowledge() {
return this.knowledgeList.filter(knowledge =>
knowledge.includes(this.searchKeyword)
);
}
@State isDarkMode: boolean = false;
// 切换深色模式
toggleTheme() {
this.isDarkMode = !this.isDarkMode;
}
通过这个五行知识分享项目,我学到了: