HarmonyOS 鸿蒙Next 日历创建日程
HarmonyOS 鸿蒙Next 日历创建日程 创建日程传了identifier字段,查询到的日程identifier=undefined。或者有什么方法可以给日程新增字段,并且可以查询到?
使用calendar.getEvents需要指定条件recurrenceRule,identifier才能查询到
参考demo
import { abilityAccessCtrl, PermissionRequestResult, Permissions } from '@kit.AbilityKit';
import { calendarManager } from '@kit.CalendarKit';
import { BusinessError } from '@kit.BasicServicesKit';
@Entry
@Component
struct CalendarPage {
calendarMgr?: calendarManager.CalendarManager;
aboutToAppear(): void {
const permissions: Permissions[] = ["ohos.permission.READ_CALENDAR", "ohos.permission.WRITE_CALENDAR"];
const atManager = abilityAccessCtrl.createAtManager();
atManager.requestPermissionsFromUser(getContext(this), permissions).then((data: PermissionRequestResult) => {
console.log(`get Permission result: ${JSON.stringify(data)}`);
})
this.calendarMgr = calendarManager.getCalendarManager(getContext(this));
}
build() {
Column() {
Button('添加日常账户').onClick(() => {
let calendar: calendarManager.Calendar | undefined = undefined;
const calendarAccount: calendarManager.CalendarAccount = {
name: 'CreateMyCalendarByCallBack',
type: calendarManager.CalendarType.LOCAL
};
try {
this.calendarMgr?.createCalendar(calendarAccount, (err: BusinessError, data: calendarManager.Calendar) => {
if (err) {
console.error(`Failed to create calendar. Code: ${err.code}, message: ${err.message}`);
} else {
console.info(`Succeeded in creating calendar, data -> ${JSON.stringify(data)}`);
calendar = data;
const date = new Date();
const recurrenceRule: calendarManager.RecurrenceRule =
{ recurrenceFrequency: calendarManager.RecurrenceFrequency.DAILY }
const event: calendarManager.Event = {
type: calendarManager.EventType.NORMAL,
title: "日历测试",
startTime: date.getTime(),
endTime: date.getTime() + 60 * 60 * 1000,
identifier: "TestEzra",
recurrenceRule: recurrenceRule
};
calendar.addEvent(event, (err: BusinessError, data: number): void => {
if (err) {
console.error(`Failed to addEvent. Code: ${err.code}, message: ${err.message}`);
} else {
console.info(`Succeeded in adding event, id -> ${data}`);
}
});
}
});
} catch (error) {
console.error(`Failed to create calendar. Code: ${error.code}, message: ${error.message}`);
}
})
Button('获取日程').onClick(() => {
this.calendarMgr?.getAllCalendars((err: BusinessError, data: calendarManager.Calendar[]) => {
if (err) {
console.log(JSON.stringify(err));
console.error(`Failed to get calendar. Code: ${err.code}, message: ${err.message}`);
} else {
console.info(`Succeeded in getting calendar, data -> ${JSON.stringify(data)}`);
console.dir(data);
const calendar = data[data.length-1];
const filter = calendarManager.EventFilter.filterByTitle('日历测试');
calendar.getEvents(filter, ['title', 'type', 'startTime', 'endTime', 'identifier', 'recurrenceRule'],)
.then((data: calendarManager.Event[]) => {
console.info(`Succeeded to filter by title, data -> ${JSON.stringify(data)}`);
})
.catch((err: BusinessError) => {
console.error(`Failed to filter by title, err -> ${JSON.stringify(err)}`);
});
}
});
})
}
.height('100%')
.width('100%')
}
}
更多关于HarmonyOS 鸿蒙Next 日历创建日程的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
在HarmonyOS(鸿蒙)系统中创建日历日程的操作通常涉及以下几个步骤,这些步骤基于鸿蒙系统的原生应用及用户界面设计:
-
打开日历应用: 在鸿蒙系统的主屏幕上,找到并点击“日历”图标以打开日历应用。
-
选择创建日程的日期: 在日历视图中,浏览或搜索到你想创建日程的具体日期,并点击该日期进入日程编辑页面。
-
设置日程详情: 在日程编辑页面,输入日程的标题、时间(开始时间和结束时间)、地点(可选)、提醒时间(可选)等关键信息。你还可以根据需要选择是否重复该日程,以及设置日程的类别或颜色。
-
保存日程: 检查所有输入的信息无误后,点击页面上的“保存”或类似按钮以完成日程的创建。
-
查看和编辑日程: 日程创建成功后,你可以在日历应用中查看它。如果需要修改或删除日程,只需在日历中找到该日程并点击进行相应操作。
如果以上步骤未能解决你的问题,或者你在操作过程中遇到任何困难,请直接联系官网客服寻求帮助。官网地址是:https://www.itying.com/category-93-b0.html,