首页 > 其他分享 >卡片数据更新

卡片数据更新

时间:2024-08-30 15:24:05浏览次数:7  
标签:string 卡片 userAge 更新 userSex width pf import 数据

一、介绍

基于鸿蒙Next模拟卡片数据数据更新

二、场景需求

电商平台产品信息更新、

客户关系管理(CRM)系统、

社交媒体用户资料更新、

健康管理系统、

项目管理工具、

金融服务应用、

教育管理系统、

在线订餐平台、

三、业务步骤

第一步:输入框输入数据

第二部:保存数据

第三步:返回桌面,点击卡片上的刷新按钮

第四步:刷新卡片数据

四、效果展示

卡片数据更新_ide


五:代码展示:

import router from '@ohos.router'
import promptAction from '@ohos.promptAction'
import preferences from '@ohos.data.preferences';
import common from '@ohos.app.ability.common';
import abilityAccessCtrl, { Permissions } from '@ohos.abilityAccessCtrl';

let context = getContext(this) as common.UIAbilityContext

@Entry
@Component
struct Index07 {
  @State userName: string = ''
  @State userAge: string = ''
  @State userSex: string = ''
  //控制器
  TextAreaController: TextAreaController = new TextAreaController()

  @Styles
  RowTextInput(){
    .width('100%')
    .margin({ top: 10, left: 10 })
  }

  //本地数据-保存
  preferencesFun() {


    preferences.getPreferences(context, "CardData", (err, pf) => {
      if (err) {
        console.error("preferences failed to code =" + err.code + ";;; message =" + err.message);
        return;
      }
      pf.put('userName', this.userName)
      pf.put('userAge', this.userAge)
      pf.put('userSex', this.userSex)
      pf.flush()
    })
  }

  build() {
    Column() {
      //主体内容-我的卡片
      Column() {
        Column() {
          TextInput({ placeholder: '请输入名字...' })
            .width('80%')
            .onChange((value: string) => {
              this.userName = value
            })
            .margin({ bottom: 20 })
            .backgroundColor(0Xeeeeee)

          TextInput({ placeholder: '请输入年龄...' })
            .width('80%')
            .onChange((value: string) => {
              this.userAge = value
            })
            .margin({ bottom: 20 })
            .backgroundColor(0Xeeeeee)

          TextInput({ placeholder: '请输入性别...' })
            .width('80%')
            .onChange((value: string) => {
              this.userSex = value
            })
            .margin({ bottom: 20 })
            .backgroundColor(0Xeeeeee)

        }.width("100%").alignItems(HorizontalAlign.Center)

        Row() {
          Button('保存到卡片')
            .onClick(() => {
              this.preferencesFun()
            })
        }.width('100%')
        .justifyContent(FlexAlign.Center)

      }
      .width('100%')
      .alignItems(HorizontalAlign.Start)
      .justifyContent(FlexAlign.Start)
      .margin({ bottom: 56 })
      .padding({ right: 12, left: 12 })

    }
  }
}


Widget_zk
let storageUpdateByMsg = new LocalStorage();

@Entry(storageUpdateByMsg)
@Component
struct UpdateByMessageCard {
  @LocalStorageProp('userName') userName: ResourceStr = "李靖婷";
  @LocalStorageProp('userAge') userAge: ResourceStr = "18";
  @LocalStorageProp('userSex') userSex: ResourceStr = "女";

  build() {
    Column() {
      Column() {
        Text(this.userName)
          .opacity(0.9)
          .fontSize(14)
        Text(this.userAge)
          .opacity(0.6)
          .fontSize(12)
        Text(this.userSex)
          .opacity(0.6)
          .fontSize(12)
      }.width('100%')
      .height(80)
      .alignItems(HorizontalAlign.Center)
      .justifyContent(FlexAlign.Center)

      Row() {
        Button('刷新')
        .width(120)
        .borderRadius(16)
        .onClick(() => {
          postCardAction(this, {
            action: 'message',
            params: { msgTest: 'messageEvent' }
          });
        })
      }.width('100%').height('40%')
      .justifyContent(FlexAlign.Center)
    }
    .width('100%')
    .height('100%')
    .alignItems(HorizontalAlign.Start)
    .backgroundImageSize(ImageSize.Cover)
  }
}

EntryFormAbility.ets

import { formBindingData, FormExtensionAbility, formInfo, formProvider } from '@kit.FormKit';
import { Want } from '@kit.AbilityKit';
import { BusinessError } from '@kit.BasicServicesKit';
import preferences from '@ohos.data.preferences';
import { hilog } from '@kit.PerformanceAnalysisKit';

const TAG: string = 'EntryFormAbility';
const DOMAIN_NUMBER: number = 0xFF00;

export default class EntryFormAbility extends FormExtensionAbility {
  onAddForm(want: Want) {
    // Called to return a FormBindingData object.
    let formData = '';
    return formBindingData.createFormBindingData(formData);
  }

  onCastToNormalForm(formId: string) {
    // Called when the form provider is notified that a temporary form is successfully
    // converted to a normal form.
  }

  onUpdateForm(formId: string) {
    // Called to notify the form provider to update a specified form.
  }

  onFormEvent(formId: string, message: string) {
    // Called when a specified message event defined by the form provider is triggered.
    hilog.info(DOMAIN_NUMBER, TAG, `FormAbility onFormEvent, formId = ${formId}, message: ${JSON.stringify(message)}`);

    preferences.getPreferences(this.context,'CardData',(err,pf)=>{

      class FormDataClass {
        userName = pf.get('userName', '')
        userAge = pf.get('userAge', '')
        userSex = pf.get('userSex', '')
      }
      let formData = new FormDataClass();
      let formInfo: formBindingData.FormBindingData = formBindingData.createFormBindingData(formData);
      formProvider.updateForm(formId, formInfo).then(() => {
        hilog.info(DOMAIN_NUMBER, TAG, 'FormAbility updateForm success.');
      }).catch((error: BusinessError) => {
        hilog.info(DOMAIN_NUMBER, TAG, `Operation updateForm failed. Cause: ${JSON.stringify(error)}`);
      })
    })

  }

  onRemoveForm(formId: string) {
    // Called to notify the form provider that a specified form has been destroyed.
  }
  onAcquireFormState(want: Want) {
    // Called to return a {@link FormState} object.
    return formInfo.FormState.READY;
  }
};


六、代码结构及原理:

1.整体结构

使用了ArkTS的装饰器语法,如@Entry和@Component组件。使用了动态卡片添加之桌面

2.状态管理:

组件使用@State装饰器定义了几个响应式状态变量

3.UI结构:

主要界面使用嵌套的Column和Row组件构建。

4.数据传递:

当点击"保存"按钮时,主页面会调用preferencesFun()回调函数来本地化保存数据,当点击"刷新"按钮时,会调用postCardAction()回调函数,回调卡片控制器的onFormEvent()函数,更新卡片数据。

标签:string,卡片,userAge,更新,userSex,width,pf,import,数据
From: https://blog.51cto.com/u_14946066/11876405

相关文章

  • Docmatix - 超大文档视觉问答数据集
    本文,我们将发布Docmatix-一个超大的文档视觉问答(DocVQA)数据集,比之前的数据集大100倍。当使用Docmatix微调Florence-2时,消融实验显示DocVQA任务的性能提高了20%。Docmatix数据集样本示例缘起于丹鼎(TheCauldron)的开发,丹鼎包含了50个数据集,旨在用于视......
  • 服务重启了,如何保证线程池中的数据不丢失?
    大家好,我是苏三,又跟大家见面了。前言最近有位小伙伴在我的技术群里,问了我一个问题:服务down机了,线程池中如何保证不丢失数据?这个问题挺有意思的,今天通过这篇文章,拿出来跟大家一起探讨一下。1什么是线程池?之前没有线程池的时候,我们在代码中,创建一个线程有两种方式:继承Thread......
  • opc da 服务器数据 转IEC61850项目案例
    目录1 案例说明 12 VFBOX网关工作原理 13 应用条件 24 查看OPCDA服务器的相关参数 25 配置网关采集opcda数据 46 用IEC61850协议转发数据 67 网关使用多个逻辑设备和逻辑节点的方法 98 在服务器上运行仰科OPCDA采集软件 109 案例总结 121 案例说明在OPCDA服务器上......
  • SQL server 数据文件物理层面迁移
    背景当前有一套SQLserver数据库(Primary+Mirror)主备环境数据盘大小不一致,且灾备环境无法对磁盘进行扩容,需要对灾备环境进行整体数据文件迁移,满足当前数据库运行。停止数据库mirro同步select'use[master]ALTERDATABASE['+db_name(database_id)+']SETPARTNERSUSPEND'froms......
  • 数据安全指南:电脑重要文件如何加密?
    在工作中,电脑是重要的办公工具,可以处理和保存大量数据。为了避免重要文件泄露,我们需要加密保护电脑重要文件。下面我们就来了解一下电脑重要文件的加密方法。EFS加密EFS是Windows系统提供的数据加密功能,基于公钥加密策略,采用透明加密方法。在加密文件时,不需要设置密码,通过......
  • MySQL-进阶篇-SQL优化(插入数据优化、主键优化、order by优化、group by优化、limit优
    文章目录1.插入数据优化1.1使用批量插入1.2批量插入数据时手动提交事务1.3按主键的顺序插入1.4大批量插入数据时使用load指令2.主键优化2.1数据组织方式2.2页分裂2.3页合并2.4主键的设计原则2.4.1降低主键的长度2.4.2使用AUTO_INCREMENT自增主键2.4.3......
  • [WPF]数据绑定时为何会出现StringFormat失效
    在数据绑定过程中,我们经常会使用StringFormat对要显示的数据进行格式化,以便获得更为直观的展示效果,但在某些情况下格式化操作并未生效,例如Button的Content属性以及ToolTip属性绑定数据进行StringFormat时是无效的。首先回顾一下StringFormat的基本用法。StringFormat的用法Str......
  • django中的数据库连接池实现
    1、第一步安装数据库连接池第三方模块pipinstalldjango-db-connection-pool2、在项目目录下的settings文件中添加下面配置DATABASES={"default":{'ENGINE':'dj_db_conn_pool.backends.mysql','NAME':'day04',#数据库名字......
  • AIGC时代,仅用合成数据训练模型到底行不行?来一探究竟 | CVPR 2024
    首个针对使用合成数据训练的模型在不同稳健性指标上进行详细分析的研究,展示了如SynCLIP和SynCLR等合成克隆模型,其性能在可接受的范围内接近于在真实图像上训练的对应模型。这一结论适用于所有稳健性指标,除了常见的图像损坏和OOD(域外分布)检测。另一方面,监督模型SynViT-B在除形状偏......
  • python使用 pyshark 库捕获数据包,附示例
    以下为您提供使用Python的pcap库捕获网络数据包的示例及相关信息:在Python中,可以使用pcap库来实现网络数据包的捕获。例如:importpcap#创建pcap实例pc=pcap.pcap()#设置过滤条件,例如捕获TCP端口为80的数据包pc.setfilter('tcpport80')#开始抓......