鸿蒙服务卡片,点击事件,传值

用来显示或者提示一些基本信息,或进行一些基本操作,不能使用重逻辑

新建:1、在ets目录上右击;2、选择Service Widget;3、Dynamic Widget

其中Static Widget,只能做静态页面,不能做数据交互

Dynamic Widget可以做动态数据,及事件处理

在entry的model.json5中,

复制代码
 "extensionAbilities": [
      {
        "name": "EntryFormAbility",
        "srcEntry": "./ets/entryformability/EntryFormAbility.ets",
        "label": "$string:EntryFormAbility_label",
        "description": "$string:EntryFormAbility_desc",
        "type": "form",
        "metadata": [
          {
            "name": "ohos.extension.form",
            "resource": "$profile:form_config"
          }
        ]
      }
    ]

就是的ability,在这个ability上,就是卡片

在EntryFormAbility中,最主要的方法:onAddForm(),就是做主要的逻辑处理

在Resources / base / profile / form_config.json中,就是卡片的配置

复制代码
{
  "forms": [
    {
      "name": "widget",
      "displayName": "$string:widget_display_name",
      "description": "$string:widget_desc",
      "src": "./ets/widget/pages/WidgetCard.ets",
      "uiSyntax": "arkts",
      "window": {
        "designWidth": 720,
        "autoDesignWidth": true
      },
      "colorMode": "auto",
      "isDynamic": true,
      "isDefault": true,
      "updateEnabled": false,
      "scheduledUpdateTime": "10:30",
      "updateDuration": 1,
      "defaultDimension": "2*2",
      "supportDimensions": [
        "2*2",
        "2*4"
      ]
    }
  ]
}

卡片可以选择2*2和2*4

作用:展示数据、唤起应用;不支持包、不支持网络请求;

在应用的model.json5中,加入权限

复制代码
    'requestPermissions': [{
      "name": 'ohos.permission.KEEP_BACKGROUND_RUNNING'
    }],
复制代码
WidgetCard.ets
复制代码
@Entry
@Component
struct WidgetCard {
  @State num: number = 0

  build() {
    Row() {
      Column() {

        Button(this.num + '++').onClick(() => {
          this.num++
          postCardAction(this, {
            action: 'call',
            abilityName: 'EntryAbility',
            params:{
              method:'updataNum',
              num:this.num
            }
          });
        })
      }.width('100%')
    }
    .height('100%')
    .onClick(() => {
      postCardAction(this, {
        action: 'router', //跳转事件
        abilityName: 'EntryAbility',

      });
    })
  }
}

应用的界面:

复制代码
import { authentication } from '@kit.AccountKit';
import { BusinessError } from '@kit.BasicServicesKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { promptAction } from '@kit.ArkUI';

@Entry
@Component
struct Index {
  @State message: string = 'Hello World';
  @StorageLink('num')
  num:number=0

  build() {
    RelativeContainer() {
      Text(this.message+this.num)
    }
    .height('100%')
    .width('100%')
  }


}
复制代码
EntryAbility.ets
复制代码
import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { window } from '@kit.ArkUI';
import { rpc } from '@kit.IPCKit';

class Params implements rpc.Parcelable{
  marshalling(dataOut: rpc.MessageSequence): boolean {
    return true
  }

  unmarshalling(dataIn: rpc.MessageSequence): boolean {
    return true
  }

}

class CaraParam{
  num:number=0
}

export default class EntryAbility extends UIAbility {
  onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate');

    this.callee.on('updataNum',(data)=>{
      let res=JSON.parse(data.readString())as CaraParam
      AppStorage.setOrCreate('num',res.num)
      return new Params()
    })
  }

//销毁监听
  onDestroy(): void {
    this.callee.off('updataNum')
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy');
  }
相关推荐
万少28 分钟前
AI编程神器!Trae+Claude4.0 简单配置 让HarmonyOS开发效率飙升 - 坚果派
前端·aigc·harmonyos
zhanshuo1 小时前
HarmonyOS UI 交互实战指南:教你用 ArkUI 打造高响应体验
harmonyos
zhanshuo2 小时前
新手也能搞定!鸿蒙开发者资源包获取与实战全流程指南
harmonyos
baobao熊7 小时前
HarmonyOS 6 云开发-用户头像上传云存储
华为·数据挖掘·harmonyos
优测云服务平台7 小时前
优测推出HarmonyOS全场景测试服务,解锁分布式场景应用卓越品质!
分布式·harmonyos
HarmonyOS小助手9 小时前
【宝藏贴】HarmonyOS官方模板优秀案例 · 第1期:便捷生活-购物中心
harmonyos·鸿蒙·鸿蒙生态
沫小北10 小时前
HarmonyOS Lottie动画库总结
前端·harmonyos
zhanshuo1 天前
如何用 ArkTS 实现丝滑又安全的表单输入验证?一篇文章讲清楚!
harmonyos
zhanshuo1 天前
掌握 ArkTS 复杂数据绑定:从双向输入到多组件状态同步
harmonyos
SuperHeroWu71 天前
【HarmonyOS】鸿蒙应用开发中常用的三方库介绍和使用示例
华为·harmonyos