40.HarmonyOS鸿蒙系统 App(ArkUI)实现页面跳转与返回

1.新建项目,默认创建inext.ets界面。

2.右键page添加第二个页面,设置page2,点击finish

设置按钮触发事件:

page2.ets按钮触发事件

index.ets

复制代码
import FaultLogger from '@ohos.faultLogger'
import promt2 from '@ohos.prompt'
import promt_action from '@ohos.promptAction'
import router from '@ohos.router';
@Entry
@Component
struct Index {
  private arr_value:string[] = ['AP1','AP2','AP3','AP4','AP5','AP6','AP7','AP8'];
  @State message: string = '调试'
  @State message2: string = '测试'
  @State message3: string = 'demo by txwtech'
  @State handlePopup2:boolean = false

 //堆叠布局示范,堆叠布局放两个弹性布局
  build() {
   // Stack({}){
   //   Column(){}.width('80%').height('70%').backgroundColor(Color.Blue)//线性布局在底层
   //   Text('堆叠布局').width('99%').height(100).margin({top:5}).backgroundColor(Color.Yellow) //text在中层
   //   Button('按钮').width('80%').height(100).margin({top:50})//按钮在最上层
   // }
    //线性布局结合弹性布局
    //使用弹性布局,可以实现子组件沿水平方向排列,两端对齐,子组件间距平分,竖直方向上子组件居中的效果
    Column(){
      Column({space:5}){
        Text('').height(30)
        Text('页面跳转demo').backgroundColor(Color.Green).fontColor(Color.White).fontSize(30).width('100%').textAlign(TextAlign.Center)
        Text('').height(30)
        Flex({direction:FlexDirection.Row,wrap:FlexWrap.NoWrap,justifyContent:FlexAlign.SpaceBetween,alignItems:ItemAlign.Center}){
          Text('文本1').width('30%').height(50).backgroundColor(Color.Blue).fontColor(Color.White)
          Text('文本2').width('30%').height(50).backgroundColor(Color.Blue).fontColor(Color.White)
          Text('文本3').width('30%').height(50).backgroundColor(Color.Blue).fontColor(Color.White)

        }
        Flex({direction:FlexDirection.Row,wrap:FlexWrap.NoWrap,justifyContent:FlexAlign.SpaceBetween,alignItems:ItemAlign.Center}){
          Text('文本4').width('30%').height(50).backgroundColor(Color.Blue).fontColor(Color.White)
          Text('文本5').width('30%').height(50).backgroundColor(Color.Blue).fontColor(Color.White)
          Text('文本6').width('30%').height(50).backgroundColor(Color.Blue).fontColor(Color.White)

        }
        Flex({direction:FlexDirection.Row,wrap:FlexWrap.NoWrap,justifyContent:FlexAlign.SpaceBetween,alignItems:ItemAlign.Center}){
          Text('文本7').width('30%').height(50).backgroundColor(Color.Blue).fontColor(Color.White)
          Text('文本8').width('30%').height(50).backgroundColor(Color.Blue).fontColor(Color.White)
          Text('文本9').width('30%').height(50).backgroundColor(Color.Blue).fontColor(Color.White)

        }
        Text('').height(30)
        Flex({direction:FlexDirection.Row,wrap:FlexWrap.NoWrap,justifyContent:FlexAlign.SpaceBetween,alignItems:ItemAlign.Center}){
          Button('按钮1').width('30%').height(50).backgroundColor(Color.Blue).fontColor(Color.White)
          Button('按钮2').width('30%').height(50).backgroundColor(Color.Blue).fontColor(Color.White)
          Button('按钮3').width('30%').height(50).backgroundColor(Color.Blue).fontColor(Color.White)

        }
        Flex({direction:FlexDirection.Row,wrap:FlexWrap.NoWrap,justifyContent:FlexAlign.SpaceBetween,alignItems:ItemAlign.Center}){
          Button('按钮4').width('30%').height(50).backgroundColor(Color.Blue).fontColor(Color.White)
          Button('按钮5').width('30%').height(50).backgroundColor(Color.Blue).fontColor(Color.White)
          Button('按钮6').width('30%').height(50).backgroundColor(Color.Blue).fontColor(Color.White)

        }

        Button('下一页').width('100%')
        .height(80)
        .fontSize(50)
        .fontColor(Color.White)
          .onClick(()=>{
            router.pushUrl({url:'pages/Page2'}).then(()=>{
              console.info('login to next page')
            }).catch((err)=>{
              console.info('catched error is ${err.code}, message is ${err.message}' )
            })
          })
          .stateStyles({
            pressed:{.backgroundColor(Color.Green)}, //按下背景颜色改变
            normal:{.backgroundColor(Color.Blue)}
          })



      }
    }





  }
}

page2.ets

复制代码
import router from '@ohos.router'
@Entry
@Component
struct Page2 {
  @State message: string = 'Hello World'

  build() {
    Row() {
      Column() {
        Text('这是第二页')
          .backgroundColor(Color.Blue)
          .fontColor(Color.White)
          .fontSize(50)
        Text(this.message)
          .fontSize(50)
          .fontWeight(FontWeight.Bold)
        Button('返回').onClick(()=>{
          try {
           router.back()
            console.info('return is ok')
          } catch (err){
            console.info('this is err',err.message)
          }

          })
          .width('100%')
          .fontSize(50)

      }
      .width('100%')
    }
    .height('100%')
  }
}
相关推荐
ldsweet12 小时前
《HarmonyOS技术精讲-Basic Services Kit》线程通信利器:Emitter实现高效线程间消息传递
华为·harmonyos
心中有国也有家12 小时前
AtomGit Flutter 鸿蒙客户端:Canvas 绘制进阶-路径、渐变与混合模式
android·javascript·flutter·华为·harmonyos
w1395485642213 小时前
鸿蒙应用开发实战【93】— 实战短信批量导入完整流程
华为·harmonyos·鸿蒙系统
Georgewu14 小时前
【HarmonyOS 三方库 】Python 三方库鸿蒙化适配 PC 完整迁移实战
harmonyos
花开彼岸天~15 小时前
鸿蒙应用开发实战【94】— 实战多卡号场景管理最佳实践
android·华为·harmonyos·鸿蒙系统
花开彼岸天~15 小时前
鸿蒙应用开发实战【97】— 应用发布AGC上架全流程
华为·harmonyos·鸿蒙系统
绝世番茄15 小时前
HarmonyOS Next 深入解析:Scroll 组件与 onScroll 滚动监听实战
华为·harmonyos·鸿蒙
2501_9197490315 小时前
华为鸿蒙免费铃声APP—小羊免费铃声
华为·harmonyos
不羁的木木15 小时前
《HarmonyOS技术精讲-NearLink Kit(星闪服务)》第3篇:数据收发——点对点数据传输实战
pytorch·华为·harmonyos
木木子2215 小时前
[特殊字符] 种菜模拟器 — 鸿蒙ArkTS完整技术解析
microsoft·华为·harmonyos