当前位置: 首页 > article >正文

鸿蒙:实现两个Page页面跳转

效果展示

这篇博文在《鸿蒙:从0到“Hello Harmony”》基础上实现两个Page页面跳转

1.构建第一个页面

第一个页面就是“Hello Harmony”,把文件名和显示内容都改一下,改成“FirstPage”,再添加一个“Next”按钮。

@Entry
@Component
struct FristPage {
  @State message1: string = "FirstPage"
  @State message2: string = 'Next'

  build() {
    Row() {
      Column() {
        Text(this.message1)
          .fontSize(30)
          .fontWeight(FontWeight.Bold)
          .height("10%")
          .margin({
            top: 0
          })

        Button(this.message2)
          .fontSize(30)
          .fontWeight(FontWeight.Bold)
          .height('5%')
          .type(ButtonType.Capsule)
          .margin({
            top: 30
          })
          .backgroundColor("#0D9FFB")
          .width('50%')
          .height('5%')
      }
      .width('100%')
    }
    .height('100%')
  }
}

在编辑窗口右上角的侧边工具栏,点击Previewer,打开预览器。第一个页面效果如下图所示:

2.构建第二个页面

1.在“entry > src > main > ets > pages”目录下,新建SecondPage.ets

同样,实现一个文本和一个Button按钮

@Entry
@Component
struct SecondPage {
  @State message1: string = 'SecondPage'
  @State message2: string = 'Back'

  build() {
    Row() {
      Column() {
        Text(this.message1)
          .fontSize(30)
          .fontWeight(FontWeight.Bold)

        Button(this.message2)
          .fontSize(30)
          .fontWeight(FontWeight.Bold)
          .height('5%')
          .type(ButtonType.Capsule)
          .margin({
            top: 30
          })
          .backgroundColor("#0D9FFB")
          .width('50%')
          .height('5%')
      }
      .width('100%')
    }
    .height('100%')
  }
}

Previewer效果:

3.配置路由

配置第二个页面的路由。

在“Project”窗口,打开“entry > src > main > resources > base > profile”,

main_pages.json文件中的“src”下配置第二个页面的路由“pages/second”

代码如下:

{
  "src": [
    "pages/FirstPage",
    "pages/SecondPage"
  ]
}

4.实现页面跳转

​页面间的导航可以通过页面路由router来实现。

页面路由router根据页面url找到目标页面,从而实现跳转。

使用页面路由需要导入router模块。

(1).第一个页面跳转到第二个页面

在第一个页面中,跳转按钮绑定onClick事件,点击按钮时跳转到第二页。

FirstPage.ets”文件的代码如下:

// @ohos.router模块功能从API version 8开始支持,请使用对应匹配的SDK
import router from '@ohos.router';

@Entry
@Component
struct FristPage {
  @State message1: string = "FirstPage"
  @State message2: string = 'Next'

  build() {
    Row() {
      Column() {
        Text(this.message1)
          .fontSize(30)
          .fontWeight(FontWeight.Bold)
          .height("10%")
          .margin({
            top: 0
          })

        Button(this.message2)
          .fontSize(30)
          .fontWeight(FontWeight.Bold)
          .height('5%')
          .type(ButtonType.Capsule)
          .margin({
            top: 30
          })
          .backgroundColor("#0D9FFB")
          .width('50%')
          .height('5%')
          .onClick(() => {
            console.info(`Succeeded in clicking the 'Next' button.`)
            try {
              router.pushUrl({ url: 'pages/SecondPage' })
              console.info('Succeeded in jumping to the second page.')
            } catch (err) {
              console.error(`Failed to jump to the second page.Code is ${err.code}, message is ${err.message}`)
            }
          })
      }
      .width('100%')
    }
    .height('100%')
  }
}

(1).第二个页面跳转到第一个页面

在第二个页面中,返回按钮绑定onClick事件,点击按钮时返回到第一页。

“SecondPage.ets”文件的示例如下:

import router from '@ohos.router';

@Entry
@Component
struct SecondPage {
  @State message1: string = 'SecondPage'
  @State message2: string = 'Back'

  build() {
    Row() {
      Column() {
        Text(this.message1)
          .fontSize(30)
          .fontWeight(FontWeight.Bold)

        Button(this.message2)
          .fontSize(30)
          .fontWeight(FontWeight.Bold)
          .height('5%')
          .type(ButtonType.Capsule)
          .margin({
            top: 30
          })
          .backgroundColor("#0D9FFB")
          .width('50%')
          .height('5%')
          .onClick(() => {
            console.info(`Succeeded in clicking the 'Next' button.`)
            try {
              router.pushUrl({ url: 'pages/FirstPage' })
              console.info('Succeeded in jumping to the second page.')
            } catch (err) {
              console.error(`Failed to jump to the second page.Code is ${err.code}, message is ${err.message}`)
            }
          })

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

5.实现效果

如开头展示


http://www.kler.cn/news/134270.html

相关文章:

  • 多线程概述
  • RTMP协议和源码解析
  • MFC/QT 一些快要遗忘的细节:
  • 本地Git项目同时推送至GitHub和Gitee
  • 蓝桥杯每日一题2023.11.18
  • Appium自动化测试:通过appium的inspector功能无法启动app的原因
  • Golang起步篇(Windows、Linux、mac三种系统安装配置go环境以及IDE推荐以及入门语法详细释义)
  • 学霸教你自学人工智能
  • 设计模式(二)-创建者模式(2)-工厂模式
  • 程序员告诉你:人工智能是什么?
  • ClickHouse SQL 查询优化
  • openssl开发详解
  • Android 13 - Media框架(14)- OpenMax(二)
  • 庖丁解牛:NIO核心概念与机制详解 04 _ 分散和聚集
  • 分类预测 | Matlab实现PSO-GRU-Attention粒子群算法优化门控循环单元融合注意力机制多特征分类预测
  • 取数游戏2(动态规划java)
  • MSSQL-逻辑级常用命令
  • 设计模式(二)-创建者模式(2-0)-简单工厂模式
  • CocoaPods podfile 文件配置
  • 丹麦能源袭击预示着更关键的基础设施成为目标
  • UOS统信操作系统QIcon::fromTheme详解
  • 翻译软件Mate Translate mac中文版介绍说明
  • NewStarCTF2023 Reverse Week3 EzDLL WP
  • nodejs+vue实验室上机管理系统的设计与实现-微信小程序-安卓-python-PHP-计算机毕业设计
  • c语言中*p1++和p1++有啥区别
  • 大数据Doris(二十五):Stream Load数据导入演示和其他导入案例
  • android适配鸿蒙系统开发
  • 说一说HTTP1.0、1.1、2.0版本区别和优化
  • c++中的String
  • 【算法】石子合并(区间dp)