HarmonyOS Next 办公应用:个人资料设置页面开发(一)

82 阅读1分钟

HarmonyOS Next 办公应用:个人资料设置页面开发

概述

在 HarmonyOS Next 办公类应用开发中,构建个人资料设置页面是常见需求,它能让用户方便地管理个人信息。下面将介绍如何开发一个功能丰富的个人资料设置页面。

核心代码功能及对应代码段

1. 数据模型定义
@Observed
class SettingMenu {
  public id: string; 
  public image: Resource; 
  public text: string; 
  public content: string;
  public type: number;
  constructor(id: string, image: Resource, text: string, content: string, type: number) {
    this.id = id;
    this.image = image;
    this.text = text;
    this.content = content;
    this.type = type;
  }
}

此代码定义了 SettingMenu 类,用于表示个人资料设置中的每个菜单项。包含唯一 id、图标、文本、内容和类型,为后续构建菜单提供了数据模型。

2. 菜单项视图组件
@Component
export struct SettingItemView {
  private image: string | Resource = ''; 
  private text: string | Resource = ''; 
  private content: string = '';
  private type: number = 0; 
  build() {
    Row() {
      Row() {
        Text(this.text)
          .fontSize(16)
          .fontColor('#99182431')
          .margin({ left: 12 })
          .layoutWeight(LAYOUT_WEIGHT)
          .lineHeight(22)
          .width('50%')
        if (this.type===1) {
          Image(this.image)
            .width(40)
            .height(40)
            .align(Alignment.Start)
            .objectFit(ImageFit.Contain)
        } else if (this.type===2) {
          Text(this.content)
            .fontSize(14)
            .fontColor('#99182431')
            .layoutWeight(LAYOUT_WEIGHT)
            .margin({ left: 70 })
            .lineHeight(22)
            .textAlign(TextAlign.Center)
        } else {
          TextInput({ placeholder: '请填写' })
            .enableKeyboardOnFocus(false)
            .fontSize(14)
            .placeholderColor(Color.Grey)
            .placeholderFont({ size: 14 })
            .fontColor(Color.Grey)
            .margin({ left: 70 })
            .layoutWeight(LAYOUT_WEIGHT)
            .backgroundColor(Color.White)
            .textAlign(TextAlign.Center)
        }
      }
      .width('100%')
      .height(48)
      .padding({
        left: 12,
        right: 12
      })
      .alignItems(VerticalAlign.Center)
    }
    .width('100%')
    .height(48)
    .alignSelf(ItemAlign.Center)
  }
}

SettingItemView 组件根据 type 的不同,展示不同的视图,如显示图标、文本内容或输入框,为每个菜单项提供了多样化的展示方式。