介紹
服務卡片是一直桌面小組件访惜,可以放置在桌面上等位置,一觸即達宙址。
服務卡片分為靜態(tài)卡片和動態(tài)卡片兩類轴脐。本文介紹靜態(tài)卡片。
創(chuàng)建
回到 DevEco曼氛,在目錄entry
右鍵豁辉,點擊創(chuàng)建 Service Widget,選擇 Static Widget
, 點擊 Next舀患。
image.png
輸入名稱徽级,選擇支持的卡片大小,點擊確定創(chuàng)建卡片聊浅。
其中 22 代表 2行2列餐抢,12 代表1行2列。
image.png
編寫卡片界面
交互
點擊事件傳參
這里使用 ArkUI 編寫界面低匙,不過不能使用點擊事件旷痕,轉而應該使用 FormLink,相關的事件在 formability 側接受顽冶,通過不同的參數(shù)欺抗,調用 router.push 打開不同的頁面。
FormLink({
action: this.ACTION_TYPE,
abilityName: this.ABILITY_NAME,
params: {
action: this.MESSAGE
}
}) {
...
}
參數(shù)接收
在 entryability
中的 onCreate
和 onNewWant
生命周期中强重,來接收參數(shù)
onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {
if(want?.parameters?.params) {
let params: Record<string, Object> = JSON.parse(want.parameters.params as string);
this.selectPage = params.action as string;
console.log("selectPage", this.selectPage);
}
}
onNewWant(want: Want, launchParam: AbilityConstant.LaunchParam): void {
console.log('onNewWant');
if (want?.parameters?.params) {
// want.parameters.params 對應 postCardAction() 中 params 內容
let params: Record<string, Object> = JSON.parse(want.parameters.params as string);
this.selectPage = params.action as string;
hilog.info(DOMAIN_NUMBER, TAG, `onNewWant selectPage: ${this.selectPage}`);
}
if (this.currentWindowStage !== null) {
this.onWindowStageCreate(this.currentWindowStage);
}
}
注意事項
1.運行時绞呈,請使用正常模式,服務卡片不支持 HotReload 间景,而且熱重載模式下卡片無法正常顯示佃声。