六盘水市网站建设_网站建设公司_导航易用性_seo优化
2025/12/22 18:17:23 网站建设 项目流程
  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免费进入

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询