HarmonyOS Next应用开发实战——多功能页面组件构建(part1)

HarmonyOS Next应用开发实战------多功能页面组件构建

文章概要

本文聚焦于HarmonyOS Next应用开发中多功能页面组件的构建。详细介绍了一个具备多种功能模块的页面组件,包括背景图展示、预约试驾与订购按钮、美图鉴赏、了解更多信息、车型活动展示以及金融计算器展示等功能的实现方式。

核心功能介绍

1. 数据初始化

在组件即将显示时,进行数据的初始化操作,包括车型活动列表和"了解更多"信息列表的处理。

typescript 复制代码
aboutToAppear(){
  this.vehicleModelActivitiesList.pushArrayData(VehicleModelActivities.getMainList())
  let tempArr: LearnMoreModelInfo[] = []
  for(let i = 0; i < this.learnMoreList.length; i++){
    tempArr.push(this.learnMoreList[i]);
    if(i % 3 === 0){  // 三个一组
      this.modificationLearnMoreList.push(tempArr);
      tempArr = [];
    }
  }
}
2. 背景图与操作按钮布局

构建一个带有背景图的页面,并且在底部添加"预约试驾"和"立即订购"按钮。

typescript 复制代码
Stack({ alignContent: Alignment.Bottom }){
  Image($r(this.imgStr))
    .width(CommonConstants.COLUMN_WIDTH).height(CommonConstants.COLUMN_HEIGHT)
  Flex({ justifyContent : FlexAlign.SpaceAround }){
    Text('预约试驾').textStyle(Color.White)
    Blank()
    Text('立即订购').textStyle(Color.Black).backgroundColor(Color.White)
  }
  .padding({
    left: CommonConstants.PUBLIC_SPACE * 2,
    right: CommonConstants.PUBLIC_SPACE * 2,
    bottom: CommonConstants.PUBLIC_SPACE * 3
  })
  .width(CommonConstants.COLUMN_WIDTH)
}
.width(CommonConstants.COLUMN_WIDTH).height(CommonConstants.COLUMN_HEIGHT)
```##### 3. 登录方式切换
支持密码登录和验证码登录两种方式,用户可以通过点击相应文本进行切换。
```typescript
if (this.isPwdLgn) {
  Text('密码登录')
    .fontSize(CommonConstants.M_FONT_SIZE)
    .fontColor($r('app.color.main_color'))
    .margin({ top: '20vp' })
    .onClick(() => {
      this.isPwdLgn = !this.isPwdLgn;
      this.isCodeLgn = !this.isCodeLgn;
    })
} else {
  Text('验证码登录')
    .fontSize(CommonConstants.M_FONT_SIZE)
    .fontColor($r('app.color.main_color'))
    .margin({ top: '20vp' })
    .onClick(() => {
      this.isCodeLgn = !this.isCodeLgn;
      this.isPwdLgn = !this.isPwdLgn;
      promptAction.showToast({
        message: '手机号格式错误',
        duration: CommonConstants.ADVERTISING_INTERVAL_TIME,
      })
      return
    })
}
4. 协议勾选与登录验证

用户需要勾选《用户协议》和《隐私条款》才能进行登录操作,登录按钮的可用性根据输入内容进行判断。

typescript 复制代码
Flex({ justifyContent: FlexAlign.Start, alignItems: ItemAlign.Center }) {
  Checkbox({ name: 'isAgree' })
    .select($$this.isAgree)
    // ...
}

Button('密码登录', { type: ButtonType.Normal })
  .enabled(isLoginClickable(this.phone))
  .onClick(() => {
    if (this.isAgree) {
      this.isPwdIptShow = !this.isPwdIptShow;
    } else {
      promptAction.showToast({
        message: '请勾选同意《用户协议》和《隐私政策》',
        duration: CommonConstants.ADVERTISING_INTERVAL_TIME,
      })
      return
    }
  })
5. 登录跳转

当用户输入有效信息并点击登录按钮后,页面会跳转到指定页面。

typescript 复制代码
Button('登录', { type: ButtonType.Normal })
  .enabled(this.isCodeLgn ? (this.password.length > 5 ?? false)
    : this.password != '')
  .onClick(() => {
    this.pageInfos.replacePath({ name: 'TabPage' }, false);
  })

通过以上核心功能的实现,开发者可以在HarmonyOS Next应用中创建一个功能丰富、交互良好的登录页面。

相关推荐
李游Leo8 小时前
HarmonyOS 7 DualCart 平行视界适配实录 04:EasyGo × 虚拟容器:商品比价双详情与分栏比例策略【鸿蒙心迹】
华为·harmonyos
李游Leo8 小时前
HarmonyOS 7 PixelBridge 原生库适配实录 06:Release 性能基线、资源释放、包体积与工程化验收【鸿蒙心迹】
华为·harmonyos
传奇开心果编程8 小时前
【ArkUI进阶练中学】第11课:安全与合规进阶
学习·ui·华为·harmonyos
熊猫钓鱼>_>10 小时前
Kotlin Multiplatform for OpenHarmony 实战:为 Reaktive 实现响应式原语适配(完整版 · 含摘要目录与技术图表)
华为·kotlin·大模型·ai编程·harmonyos·适配·reaktive
李游Leo13 小时前
HarmonyOS 7 + Spatial Recon Kit-C++:3DGS 高斯参数的非有限值隔离与可渲染性门禁【鸿蒙心迹】
开发语言·c++·3d·harmonyos
传奇开心果编程13 小时前
【ArkUI进阶练中学】第12课:应用架构演进与遗留系统迁移
学习·ui·华为·harmonyos
传奇开心果编程15 小时前
【ArkUI进阶练中学】第13课:元服务与卡片开发
学习·ui·华为·harmonyos
李游Leo16 小时前
HarmonyOS 7 InsightBoard 多形态适配实录 06:ArkUI × 多设备回归:布局抖动、监听释放与全形态验收【鸿蒙心迹】
回归·kotlin·harmonyos
李游Leo16 小时前
HarmonyOS 7 PixelBridge 原生库适配实录 04:Native Buffer Pool、批任务背压与取消链路【鸿蒙心迹】
华为·harmonyos