文章目录
- 前言
- 应用场景
- 设计思路
- 实现过程
- 预填数据的状态管理
- 头像区域
- 带预填值的输入框
- 保存按钮
- 完整代码
- 效果调优
- 检测是否有修改
- 离开页面的确认
- 写在最后
前言
个人信息编辑页跟注册表单最大的区别是什么?预填数据。用户打开这个页面时,输入框里已经有内容了——他的名字、简介、地址,都是之前填过的。所以这个页面的核心挑战不是"怎么让用户填",而是"怎么让用户改"。
今天来实现一个完整的个人信息编辑页,包含头像区域、五个文本字段和保存按钮。
应用场景
个人信息编辑页在各种 App 中都很常见:
- 社交 App 的个人资料
- 企业内部系统的员工档案
- 电商平台的收货信息管理
- 内容平台的创作者资料
设计思路
页面分为三个区域:
- 头像区— 圆形头像 + "点击更换"提示
- 信息区— 五个带预填值的输入框
- 操作区— 保存按钮
信息区的字段:
| 字段 | 预填值 | 说明 |
|------|-------|------|
| 显示名称 | 张三 | 用户昵称 |
| 个人简介 | 前端开发工程师 | 一句话介绍 |
| 所在地 | 北京 | 城市/地区 |
| 公司 | 某某科技有限公司 | 工作单位 |
| 个人网站 | https://example.com | 外部链接 |
实现过程
预填数据的状态管理
@StateshowName:string='张三'@Statebio:string='前端开发工程师'@Statelocation:string='北京'@Statecompany:string='某某科技有限公司'@Statewebsite:string='https://example.com'状态变量的初始值就是预填数据。实际项目中,这些数据通常从接口获取,在aboutToAppear生命周期里赋值。
头像区域
Column(){Text('头像').fontSize(13).fontColor('#999999').margin({bottom:8})Circle({width:64,height:64}).fill('#E0E0E0').margin({bottom:4})Text('点击更换头像').fontSize(11).fontColor('#4D96FF')}.width('100%').alignItems(HorizontalAlign.Center).margin({bottom:20})头像用了Circle组件画一个灰色圆形占位。下面的"点击更换头像"用主题色,暗示可交互。实际项目中可以在Circle上叠加Image组件显示真实头像,点击时调用系统相册选择图片。
带预填值的输入框
Column(){Text('显示名称').fontSize(13).fontColor('#999999').margin({bottom:6})TextInput({text:this.showName}).width('100%').height(42).backgroundColor('#F8F8F8').borderRadius(8).padding({left:12}).onChange((v:string)=>{this.showName=v})}.margin({bottom:14})关键点是TextInput({ text: this.showName })——text参数传入了预填值,输入框会直接显示"张三"。用户修改时,onChange回调更新状态变量。
五个输入框的写法完全一样,只是绑定的状态变量不同。
保存按钮
Button('保存修改').width('100%').height(48).backgroundColor('#4D96FF').borderRadius(24).fontColor('#FFFFFF').fontSize(16).onClick(()=>{AlertDialog.show({title:'保存成功',message:'个人信息已更新',confirm:{value:'确定',action:()=>{}}})})保存按钮的回调里,实际项目应该调接口提交数据。
完整代码
@Entry@Componentstruct ProfileEditForm{@StateshowName:string='张三'@Statebio:string='前端开发工程师'@Statelocation:string='北京'@Statecompany:string='某某科技有限公司'@Statewebsite:string='https://example.com'build(){Column(){Scroll(){Column(){Text('个人信息').fontSize(20).fontWeight(FontWeight.Bold).fontColor('#333333').margin({bottom:24})// 头像Column(){Text('头像').fontSize(13).fontColor('#999999').margin({bottom:8})Circle({width:64,height:64}).fill('#E0E0E0').margin({bottom:4})Text('点击更换头像').fontSize(11).fontColor('#4D96FF')}.width('100%').alignItems(HorizontalAlign.Center).margin({bottom:20})// 字段Column(){Text('显示名称').fontSize(13).fontColor('#999999').margin({bottom:6})TextInput({text:this.showName}).width('100%').height(42).backgroundColor('#F8F8F8').borderRadius(8).padding({left:12}).onChange((v:string)=>{this.showName=v})}.margin({bottom:14})Column(){Text('个人简介').fontSize(13).fontColor('#999999').margin({bottom:6})TextInput({text:this.bio}).width('100%').height(42).backgroundColor('#F8F8F8').borderRadius(8).padding({left:12}).onChange((v:string)=>{this.bio=v})}.margin({bottom:14})Column(){Text('所在地').fontSize(13).fontColor('#999999').margin({bottom:6})TextInput({text:this.location}).width('100%').height(42).backgroundColor('#F8F8F8').borderRadius(8).padding({left:12}).onChange((v:string)=>{this.location=v})}.margin({bottom:14})Column(){Text('公司').fontSize(13).fontColor('#999999').margin({bottom:6})TextInput({text:this.company}).width('100%').height(42).backgroundColor('#F8F8F8').borderRadius(8).padding({left:12}).onChange((v:string)=>{this.company=v})}.margin({bottom:14})Column(){Text('个人网站').fontSize(13).fontColor('#999999').margin({bottom:6})TextInput({text:this.website}).width('100%').height(42).backgroundColor('#F8F8F8').borderRadius(8).padding({left:12}).onChange((v:string)=>{this.website=v})}.margin({bottom:24})Button('保存修改').width('100%').height(48).backgroundColor('#4D96FF').borderRadius(24).fontColor('#FFFFFF').fontSize(16).onClick(()=>{AlertDialog.show({title:'保存成功',message:'个人信息已更新',confirm:{value:'确定',action:()=>{}}})})}.width('100%')}.width('100%').padding(24).backgroundColor('#FFFFFF').borderRadius(16)}.width('100%').height('100%').backgroundColor('#F5F6FA').padding(16)}}效果调优
检测是否有修改
用户什么都没改就点保存,没必要调接口。可以做一个"脏检查":
@StateoriginalData:string=''aboutToAppear(){this.originalData=JSON.stringify({name:this.showName,bio:this.bio,location:this.location,company:this.company,website:this.website})}isDirty():boolean{constcurrent=JSON.stringify({name:this.showName,bio:this.bio,location:this.location,company:this.company,website:this.website})returncurrent!==this.originalData}保存按钮根据isDirty()的结果来决定是否启用。
离开页面的确认
如果用户有未保存的修改就点返回,弹一个确认框:
onBackPress():boolean{if(this.isDirty()){AlertDialog.show({title:'未保存的修改',message:'确定要放弃编辑吗?',confirm:{value:'放弃',action:()=>{router.back()}},cancel:()=>{}})returntrue// 阻止默认返回}returnfalse}写在最后
个人信息编辑页的核心是"预填 + 修改 + 保存"。跟注册表单不同的是,你需要关注数据从哪里来(接口加载)、怎么判断有没有修改(脏检查)、离开时是否需要提醒(未保存确认)。
把这些细节做好了,你的个人信息编辑页就从一个"能用的表单"升级成了一个"好用的编辑体验"。