当前位置: 首页 > news >正文

HarmonyOS简易个人信息管理助手

  1. 功能特点:
    • 添加新的联系人信息
    • 编辑现有联系人信息
    • 删除联系人
    • 查看详细信息
    • 包含姓名、年龄、电话、邮箱、地址、备注等字段
  2. UI组件说明:
    • 顶部标题栏
    • 添加联系人按钮
    • 联系人列表(卡片式显示)
    • 编辑对话框(弹窗形式)
  3. 操作方式:
    • 点击"添加新联系人"打开添加对话框
    • 点击列表项的"编辑"按钮修改信息
    • 点击"删除"按钮删除联系人
    • 点击"详情"按钮查看完整信息
    • 完整代码
    `// 个人信息管理助手
    // 文件名:PersonalInfoManager.ets

import promptAction from '@ohos.promptAction';
import common from '@ohos.app.ability.common';

// 个人信息数据模型
class PersonInfo {
name: string = '';
age: number = 0;
phone: string = '';
email: string = '';
address: string = '';
notes: string = '';

constructor(name: string = '', age: number = 0, phone: string = '',
email: string = '', address: string = '', notes: string = '') {
this.name = name;
this.age = age;
this.phone = phone;
this.email = email;
this.address = address;
this.notes = notes;
}
}

@Entry
@Component
struct PersonalInfoManager {
// 当前编辑的信息
@State currentInfo: PersonInfo = new PersonInfo();

// 信息列表
@State infoList: PersonInfo[] = [
new PersonInfo('张三', 25, '13800138000', 'zhangsan@example.com', '北京市海淀区', '同事'),
new PersonInfo('李四', 30, '13900139000', 'lisi@company.com', '上海市浦东新区', '朋友')
];

// 控制编辑对话框显示
@State showEditDialog: boolean = false;
@State isEditing: boolean = false;
@State editIndex: number = -1;

// 构建UI
build() {
Column() {
// 标题
Text('个人信息管理助手')
.fontSize(24)
.fontWeight(FontWeight.Bold)
.margin({ top: 20, bottom: 20 });

  // 添加按钮Button('添加新联系人').width('90%').height(50).fontSize(18).backgroundColor('#007DFF').margin({ bottom: 20 }).onClick(() => {this.currentInfo = new PersonInfo();this.isEditing = false;this.showEditDialog = true;});// 信息列表List({ space: 10 }) {ForEach(this.infoList, (item: PersonInfo, index: number) => {ListItem() {this.infoItem(item, index);}}, (item: PersonInfo) => item.name + item.phone)}.width('100%').layoutWeight(1).divider({ strokeWidth: 1, color: '#F1F1F1' })
}
.width('100%')
.height('100%')
.padding(10)
.backgroundColor('#F5F5F5')// 编辑对话框
if (this.showEditDialog) {this.editDialog();
}

}

// 信息列表项组件
@Builder
infoItem(item: PersonInfo, index: number) {
Column({ space: 5 }) {
// 姓名和年龄
Row() {
Text(item.name)
.fontSize(20)
.fontWeight(FontWeight.Bold)
.layoutWeight(1);

    Text(item.age.toString() + '岁').fontSize(16).fontColor('#666');}.width('100%').padding({ left: 15, right: 15, top: 10 });// 电话if (item.phone) {Row() {Image($r('app.media.ic_phone')).width(20).height(20).margin({ right: 10 });Text(item.phone).fontSize(16);}.width('100%').padding({ left: 15, right: 15 });}// 邮箱if (item.email) {Row() {Image($r('app.media.ic_email')).width(20).height(20).margin({ right: 10 });Text(item.email).fontSize(16);}.width('100%').padding({ left: 15, right: 15 });}// 操作按钮Row({ space: 10 }) {Button('编辑').width(80).height(36).fontSize(14).backgroundColor('#4CAF50').onClick(() => {this.currentInfo = { ...item };this.editIndex = index;this.isEditing = true;this.showEditDialog = true;});Button('删除').width(80).height(36).fontSize(14).backgroundColor('#FF5722').onClick(() => {this.deleteInfo(index);});Button('详情').width(80).height(36).fontSize(14).backgroundColor('#2196F3').onClick(() => {this.showDetail(item);});}.width('100%').justifyContent(FlexAlign.Center).margin({ top: 10, bottom: 10 });
}
.width('100%')
.backgroundColor('#FFFFFF')
.borderRadius(10)
.padding({ bottom: 10 })
.shadow({ radius: 5, color: '#E0E0E0', offsetX: 2, offsetY: 2 })

}

// 编辑对话框组件
@Builder
editDialog() {
Column() {
// 对话框标题
Text(this.isEditing ? '编辑信息' : '添加信息')
.fontSize(20)
.fontWeight(FontWeight.Bold)
.margin({ top: 20, bottom: 20 });

  // 输入表单TextInput({ text: this.currentInfo.name, placeholder: '请输入姓名' }).width('90%').height(50).fontSize(16).margin({ bottom: 10 }).onChange((value: string) => {this.currentInfo.name = value;});TextInput({ text: this.currentInfo.age.toString(), placeholder: '请输入年龄' }).width('90%').height(50).fontSize(16).margin({ bottom: 10 }).type(InputType.Number).onChange((value: string) => {this.currentInfo.age = parseInt(value) || 0;});TextInput({ text: this.currentInfo.phone, placeholder: '请输入电话' }).width('90%').height(50).fontSize(16).margin({ bottom: 10 }).onChange((value: string) => {this.currentInfo.phone = value;});TextInput({ text: this.currentInfo.email, placeholder: '请输入邮箱' }).width('90%').height(50).fontSize(16).margin({ bottom: 10 }).onChange((value: string) => {this.currentInfo.email = value;});TextInput({ text: this.currentInfo.address, placeholder: '请输入地址' }).width('90%').height(50).fontSize(16).margin({ bottom: 10 }).onChange((value: string) => {this.currentInfo.address = value;});TextInput({ text: this.currentInfo.notes, placeholder: '备注信息' }).width('90%').height(60).fontSize(16).margin({ bottom: 20 }).onChange((value: string) => {this.currentInfo.notes = value;});// 操作按钮Row({ space: 20 }) {Button('取消').width(120).height(45).fontSize(16).backgroundColor('#9E9E9E').onClick(() => {this.showEditDialog = false;});Button(this.isEditing ? '保存' : '添加').width(120).height(45).fontSize(16).backgroundColor('#007DFF').onClick(() => {this.saveInfo();});}.margin({ bottom: 20 });
}
.width('90%')
.backgroundColor('#FFFFFF')
.borderRadius(15)
.shadow({ radius: 20, color: '#CCCCCC' })
.position({ x: '5%', y: '5%' })

}

// 保存信息
saveInfo() {
if (!this.currentInfo.name.trim()) {
promptAction.showToast({ message: '请输入姓名', duration: 2000 });
return;
}

if (this.isEditing && this.editIndex >= 0) {// 更新现有信息this.infoList[this.editIndex] = { ...this.currentInfo };promptAction.showToast({ message: '信息更新成功', duration: 2000 });
} else {// 添加新信息this.infoList.push({ ...this.currentInfo });promptAction.showToast({ message: '信息添加成功', duration: 2000 });
}this.showEditDialog = false;

}

// 删除信息
deleteInfo(index: number) {
promptAction.showDialog({
title: '确认删除',
message: '确定要删除这条信息吗?',
buttons: [
{ text: '取消', color: '#666666' },
{ text: '确定', color: '#FF5722' }
]
}).then((result) => {
if (result.index === 1) {
this.infoList.splice(index, 1);
promptAction.showToast({ message: '删除成功', duration: 2000 });
}
});
}

// 显示详细信息
showDetail(item: PersonInfo) {
let detail = 姓名:${item.name}\n;
detail += 年龄:${item.age}岁\n;
detail += 电话:${item.phone}\n;
detail += 邮箱:${item.email}\n;
detail += 地址:${item.address}\n;
detail += 备注:${item.notes};

promptAction.showDialog({title: '详细信息',message: detail,buttons: [{ text: '关闭', color: '#007DFF' }]
});

}
}`
想入门鸿蒙开发又怕花冤枉钱?别错过!现在能免费系统学 -- 从 ArkTS 面向对象核心的类和对象、继承多态,到吃透鸿蒙开发关键技能,还能冲刺鸿蒙基础 +高级开发者证书,更惊喜的是考证成功还送好礼!快加入我的鸿蒙班,一起从入门到精通,班级链接:点击 https://developer.huawei.com/consumer/cn/training/classDetail/b7365031334e4353a9a0fd6785bb0791?type=1?ha_source=hmosclass&ha_sourceId=89000248免费进入

http://www.jsqmd.com/news/124440/

相关文章:

  • 深蓝词库转换:跨平台词库管理终极解决方案
  • 从零实现UART协议发送时序:8位数据位实战案例
  • E-Hentai下载器完整指南:快速掌握图片批量下载技巧
  • 如何快速解决ComfyUI模型路径冲突:3种简单方法
  • 小红书下载工具XHS-Downloader:解决内容保存难题的智能方案
  • Windows系统清理工具终极指南:快速上手C盘瘦身神器
  • HarmonyOS应用开发—页面路由
  • Qt - TCP传输数据
  • 零基础入门:Arduino Uno R3开发板连接心率传感器
  • BBDown终极指南:轻松下载B站视频的完整教程
  • 小米运动自动数据同步方案:3分钟搞定多平台步数更新
  • Windows Cleaner:专业解决C盘空间不足的完整系统优化方案
  • Godot游戏资源提取终极指南:从零基础到精通PCK文件解包
  • 小红书下载终极指南:3分钟搞定无水印批量下载
  • 5步掌握MTEX:材料微观结构分析的终极指南
  • 突破Windows远程连接限制:RDP Wrapper实战解密
  • MTEX工具箱:材料科学家的晶体学分析利器
  • HarmonyOS简易个人记账本
  • 一次意外断连引发的MTK设备修复探索之旅
  • 智能引导重构开发模式:零代码三天交付,资深架构师的效率革命
  • 跨平台输入法词库转换:一键迁移你的个性化输入习惯
  • 深蓝词库转换:零基础掌握输入法词库跨平台迁移终极指南
  • 微信网页版访问优化完全指南:告别频繁掉线和加载失败
  • ComfyUI-Manager完整使用指南:从安装到高级配置
  • 20251222周一日记
  • Windows Cleaner:免费快速解决C盘爆满的终极系统优化工具
  • Genshin FPS Unlocker 终极指南:轻松突破60帧限制
  • 炉石传说佣兵战记自动化脚本完整指南:3步告别重复劳动
  • 2025年南宁服务好的表冷器定制厂家怎么选择,新风机组/空调机组/高大空间冷暖风机/翅片管/工业暖风机/乏风取热箱/散热器表冷器厂商推荐排行 - 品牌推荐师
  • 炉石传说佣兵战记自动化脚本完整教程:告别手动操作的终极方案