鸿蒙启动页开发

鸿蒙启动页开发

1.1 更改应用名称和图标

1.更改应用图标

  1. 找到moudle.json5文件,找到应用启动的EntryAbility下面的icon,将原来的图标改成自己设置的即可

2.更改应用名称

3.效果展示

2.1 广告页面开发

3.1 详细介绍

3.1.1 启动页面

ts 复制代码
import { PrivacyDialog } from '../views/components/PrivacyDialog'
import { router, window } from '@kit.ArkUI'
import { preferences } from '@kit.ArkData'
import { common } from '@kit.AbilityKit'

@Entry
@Component
struct launchPage {
  //获取应用的上下文
  context: common.UIAbilityContext = getContext(this) as common.UIAbilityContext
  //获取页面的上下文
  // context:Context = getContext()
  @State
  isStore: boolean = true

  aboutToAppear(): void {
    window.getLastWindow(getContext()).then(win => {
      win.setWindowLayoutFullScreen(true)
    })
    const store = preferences.getPreferencesSync(this.context, {
      'name': 'infoStore'
    })
    this.isStore = store.getSync('isStore', true) as boolean
    console.info(`userAction: ${this.isStore}`)
  }

  async saveInfo() {
    const store = preferences.getPreferencesSync(this.context, {
      'name': 'infoStore'
    })
    store.putSync('isStore', false)
    store.flush()
  }

  //在页面出现之后:需要展示弹窗
  onPageShow(): void {
    //isStore用于定义是否保存的标识  如果显示true,就不用打开自定义弹窗,直接跳到广告界面
    // promptAction.showToast({
    //   message: this.isStore+''
    // })
    if (this.isStore) {
      this.controller.open()
    } else {
      //跳到广告界面
      router.pushUrl({
        url: 'pages/AdvertisementPage'
      })
    }

  }

  // context:Context:getContext()
  controller: CustomDialogController = new CustomDialogController({
    builder: PrivacyDialog({
      cancel: () => {
        this.context?.terminateSelf()
        //不同意关闭应用

      },
      confirm: () => { //当你点击同意,就会调用saveInfo方法
        this.saveInfo()
        //TODO 跳到广告页面
        router.pushUrl({
          url: 'pages/AdvertisementPage'
        })
      },

    }),
    alignment: DialogAlignment.Bottom, //基准位置
    // offset:{dx:30,dy:30}
  }
    //设置弹窗位置

  )

  build() {
    Stack() {
      //设置第一层
      Image($r('app.media.back'))
      Column({ space: 10 }) {
        Image($r('app.media.study_app'))
          .width(80)
          .aspectRatio(1)
        Text('健康学习')
          .fontSize(20)
          .fontWeight(FontWeight.Bold)
        Text('学习总有新玩法')
          .fontWeight(FontWeight.Bold)
      }.width('100%')
      .height('100%')
      .padding({
        top: 100
      })
    }
    .height('100%')
    .width('100%')
  }
}

3.1.2 自定义弹层

ts 复制代码
import { router } from '@kit.ArkUI'

@Preview
@CustomDialog
export struct PrivacyDialog {
  controller:CustomDialogController = new CustomDialogController({builder: ''})
  //定义两个用来接收的方法
  cancel:Function = ()=>{}
  confirm:Function = ()=>{}
  build() {

    Column(){
      Text('欢迎使用我的应用')
      Text('我们充分尊重用户的隐私权,并按照法律要求和业界成熟的安全标准,为您的个人信息提供相应的安全保护措施。')
      Text('协议隐私保护声明')
        .fontColor('#007fdd')
        .onClick(()=>{
          // TODO
          router.pushUrl({url:''})
        })
      Text('(以下简称为"本声明")以便您了解我们如何搜集、使用、披露、保护、存储、及传输您的个人数据。请您仔细阅读本声明。如您有任何疑问,请告知我们。')
      Row(){
        Text('不同意').fontColor('#007fdd')
          .onClick(()=>{
            this.controller.close()   //点击不同意,关闭
            this.cancel()
          })
        Blank()
        Text('同意').fontColor('#007fdd')
          .onClick(()=>{
            this.controller.close()
            this.confirm()
          })
      }.width('70%')
    }

  }
}

3.1.3 广告页面

ts 复制代码
import { router, window } from '@kit.ArkUI'

@Entry
@Component
struct AdvertisementPage {
  @State time: number = 5
  @State timeID: number = -1

  aboutToAppear(): void {
    window.getLastWindow(getContext()).then(win => {
      win.setWindowLayoutFullScreen(true)
    })
  }

  onPageShow(): void {

    this.timeID = setInterval(() => {
      console.log('test', this.time)
      if (this.time > 0) {
        this.time--
      } else if (this.time == 0) {
        clearTimeout(this.timeID)
        router.pushUrl({
          url: 'pages/Index'
        })
      }

    }, 1000)


  }

  build() {

    Stack() {
      Image($r('app.media.back'))
      Row() {
        Text('跳过广告' + this.time + 's')
          .fontSize(20)
          .backgroundColor('rgba(0,0,0,0.1)')
          .padding(8)
          .borderRadius(30)
      }
      .width('100%')
      .height('100%')
      .justifyContent(FlexAlign.End)
      .alignItems(VerticalAlign.Top)
      .padding({
        top: 30,
        right: 30
      })

      Row({ space: 10 }) {
        Image($r('app.media.study_app'))
          .width(80)
          .aspectRatio(1)
        Column({ space: 5 }) {
          Text('健康学习')
            .fontSize(30)
          Text('学习总有新玩法')
            .fontSize(20)
        }.height(80)
        .justifyContent(FlexAlign.Start)

      }
      .width('100%')
      .height('100%')
      .justifyContent(FlexAlign.Center)
      .alignItems(VerticalAlign.Bottom)
      .padding({
        bottom: 16
      })

    }

    .height('100%')
    .width('100%')

  }
}
相关推荐
讯方洋哥11 小时前
HarmonyOS App开发——鸿蒙ArkTS基于首选项引导页的集成和应用
华为·harmonyos
左手厨刀右手茼蒿17 小时前
Flutter 三方库 all_lint_rules_community 的鸿蒙化适配指南 - 在鸿蒙系统上构建极致、严谨、基于全量社区 Lint 规则的工业级静态代码质量与安全审计引擎
flutter·harmonyos·鸿蒙·openharmony·all_lint_rules_community
雷帝木木17 小时前
Flutter for OpenHarmony:Flutter 三方库 cbor 构建 IoT 设备的极致压缩防窃协议(基于标准二进制 JSON 表达格式)
网络·物联网·flutter·http·json·harmonyos·鸿蒙
王码码203517 小时前
Flutter 三方库 servicestack 的鸿蒙化适配指南 - 实现企业级 Message-based 架构集成、支持强类型 JSON 序列化与跨端服务调用同步
flutter·harmonyos·鸿蒙·openharmony·message-based
里欧跑得慢17 小时前
Flutter 三方库 jsonata_dart 的鸿蒙化适配指南 - 实现高性能的 JSON 数据查询与转换、支持 JSONata 表达式引擎与端侧复杂数据清洗
flutter·harmonyos·鸿蒙·openharmony·jsonata_dart
chenyingjian18 小时前
鸿蒙|性能优化-内存及其他优化
harmonyos
总有刁民想爱朕ha18 小时前
haihong Os 鸿蒙开源版开发一个pc版软件应用(1)
华为·开源·harmonyos
路-buan19 小时前
华为eNSP:三种配置防火墙的方式
华为
爱网络爱Linux21 小时前
华为 HCIA-Datacom考试版本升级 V2.0!2026年7月起考新版
华为·hcie·hcip·h12-811·华为数通认证·hcia datacom·微信公众号:厦门微思网络
弓.长.1 天前
ReactNative for OpenHarmony项目鸿蒙化三方库:react-native-masked-view — 遮罩视图组件
react native·react.js·harmonyos