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

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

新建: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');
  }
相关推荐
OH_TPC4 小时前
@ohos/axios 源码解读(二):适配层是一场翻译,而翻译要处理四类错位
harmonyos
fleaxin6 小时前
华为 Atlas 300V 兼容性测试报告
华为·pve·atlas
腾科IT教育7 小时前
HCIP-Security 华为Portal认证技术与配置解析
网络·华为·portal·hcip-security
CV工程师丁Sir9 小时前
ArkWeb 手记 11:Web 组件与原生动画的协同
harmonyos·混合开发·arkweb
李&@杰9 小时前
交换机网络环路导致MAC漂移及业务中断故障处理
网络·华为
CV工程师丁Sir9 小时前
ArkWeb 手记 12:Web 组件内存泄漏排查实录
性能优化·harmonyos·arkweb
用户29469405448161 天前
DSH Desktop - Electron on HarmonyOS Edition
harmonyos·deepseek
IsITGirl1 天前
HarmonyOS 鸿蒙 WuKong 稳定性测试报错 Failed to connect to AAMS(已解决)
华为·harmonyos
万物智能信息科技1 天前
鸿蒙RS232驱动开发—【万物智能之开源鸿蒙OpenHarmony系统实战开发系列教程】
驱动开发·华为·开源·harmonyos·鸿蒙
less_121381 天前
鸿蒙 WPS Open SDK 接入指南:凭据申请、HAR 集成与 registerApp 校验
华为·harmonyos·wps