1. 程式人生 > 其它 >3️⃣通過編寫計算器學習ArkUI元件

3️⃣通過編寫計算器學習ArkUI元件

注:轉自Tuer白曉明,原文地址:https://ost.51cto.com/posts/10480

本系列文章共有五篇,細緻描述瞭如何學習ArkUI元件實現計算器的功能,以下為正文

3.5 Row容器元件

在3.4小節中,自定義左側帶圖示的按鈕時,我們使用了Row容器元件,Row容器元件是什麼呢?
Row容器元件稱為沿水平方向佈局容器,Column容器元件是沿垂直方向佈局容器,我將兩者都稱之為線性佈局容器。
Row容器元件的用法和Column容器元件的用法類似。

@Entry
@Component
struct RowExample {
  build() {
    Flex({direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center}) {
      Text('橫向子元件佈局間距').fontSize(14).fontColor('#CCCCCC').width('90%')
      Text('居中對齊,預設對齊方式,可以不寫').fontSize(14).fontColor('#CCCCCC').width('90%')
      Row({space: 4}) {
        Text('A').width('50%').height('100%')
          .fontSize(16).backgroundColor('#D5D5D5').textAlign(TextAlign.Center)
        Text('B').width('50%').height('100%')
          .fontSize(16).backgroundColor('#E5E5E5').textAlign(TextAlign.Center)
      }
      .height(50)
      .width(300)

      Text('底部對齊').fontSize(14).fontColor('#CCCCCC').width('90%')
      Row() {
        Text('A').width('50%').height(50)
          .fontSize(16).backgroundColor('#D5D5D5').textAlign(TextAlign.Center)
        Text('B').width('50%').height(60)
          .fontSize(16).backgroundColor('#E5E5E5').textAlign(TextAlign.Center)
      }.alignItems(VerticalAlign.Bottom).width('90%').height(100)

      Text('頂部對齊').fontSize(14).fontColor('#CCCCCC').width('90%')
      Row() {
        Text('A').width('50%').height(50)
          .fontSize(16).backgroundColor('#D5D5D5').textAlign(TextAlign.Center)
        Text('B').width('50%').height(60)
          .fontSize(16).backgroundColor('#E5E5E5').textAlign(TextAlign.Center)
      }.alignItems(VerticalAlign.Top).width('90%').height(100)
    }
    .width('100%')
    .height('100%')
  }
}
 

3.6 實現頁面跳轉

通過對容器元件、元件、裝飾器的瞭解,在3.4小節實現了標題欄區域的功能按鈕佈局,如何通過點選按鈕進入到繫結的頁面呢?本小節將繼續帶大家一起了解頁面跳轉(也稱路由跳轉)。
路由跳轉有兩種形式:通過 路由容器元件Navigator 或者 路由RouterAPI介面 來實現頁面間的跳轉。

3.6.1 Navigator路由容器元件

Navigator路由容器元件用於包裝元件,使其具備路由跳轉能力,比如包含Text文字元件並設定樣式,使其能夠提供與HTMLa標籤相似的功能。通過targettype屬性控制跳轉目標頁面及路由方式。

// navigationExample.ets
@Entry
@Component
struct NavigationExample {
  build() {
    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
      Navigator({target: 'pages/simple/routerApiExample', type: NavigationType.Push}) {
        Text('跳轉到RouterApiExample頁面')
          .fontSize(16)
          .fontColor('#FFFFFF')
          .fontWeight(FontWeight.Bold)
          .backgroundColor('#cccccc')
          .height(54)
          .padding(8)
          .borderRadius(8)
      }
      .margin({bottom: 12})

      Navigator({target: 'pages/simple/routerApiExample', type: NavigationType.Replace}) {
        Text('使用RouterApiExample頁面替換當前頁')
          .fontSize(16)
          .fontColor('#FFFFFF')
          .fontWeight(FontWeight.Bold)
          .backgroundColor('#cccccc')
          .height(54)
          .padding(8)
          .borderRadius(8)
      }
    }
    .width('100%')
    .height('100%')
  }
}

// routerExample.ets
@Entry
@Component
struct RouterApiExample {
  build() {
    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
      Navigator({target: 'pages/simple/navigationExample', type: NavigationType.Back}) {
        Text('返回到NavigationExample頁面')
          .fontSize(16)
          .fontColor('#FFFFFF')
          .fontWeight(FontWeight.Bold)
          .backgroundColor('#cccccc')
          .height(54)
          .padding(8)
          .borderRadius(8)
      }
    }
    .width('100%')
    .height('100%')
  }
}
 


說明:

  • 點選【跳轉到RouterApiExample頁面】按鈕,跳轉頁面;
  • 點選【返回NavigationExample頁面】按鈕,返回頁面;
  • 點選【使用RouterApiExample頁面替換當前頁】按鈕,跳轉頁面,銷燬當前頁,無法返回。

3.6.2 RouterAPI路由介面

API介面也提供了頁面路由功能,需要在相應的頁面引入模組,並通過元件的onClick方法進行頁面跳轉,使用需要在頁面頂部引入import router from '@system.router'

API 描述
router.push 跳轉到應用內指定頁面,相當於Navigation元件設定type
值為NavigationType.Push
router.replace 用應用內的某個頁面替換當前頁面,並銷燬被替換的頁面,相當於Navigation元件設定type值為NavigationType.Replace
router.back 返回上一頁面或指定的頁面,相當於Navigation元件設定type值為NavigationType.Back
其他 用到再介紹
// navigationExample.ets
import router from '@system.router';

@Entry
@Component
struct NavigationExample {
  build() {
    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
      Text('Navigator路由容器元件').fontSize(14).fontColor('#CCCCCC').width('90%')
      Navigator({target: 'pages/simple/routerApiExample', type: NavigationType.Push}) {
        Text('跳轉到RouterApiExample頁面')
          .fontSize(16)
          .fontColor('#FFFFFF')
          .fontWeight(FontWeight.Bold)
          .backgroundColor('#cccccc')
          .height(54)
          .padding(8)
          .borderRadius(8)
      }
      .margin({bottom: 12})

      Navigator({target: 'pages/simple/routerApiExample', type: NavigationType.Replace}) {
        Text('使用RouterApiExample頁面替換當前頁')
          .fontSize(16)
          .fontColor('#FFFFFF')
          .fontWeight(FontWeight.Bold)
          .backgroundColor('#cccccc')
          .height(54)
          .padding(8)
          .borderRadius(8)
      }
      .margin({bottom: 12})

      Text('router路由Api').fontSize(14).fontColor('#CCCCCC').width('90%')
      Text('返回到RouterApiExample頁面')
        .fontSize(16)
        .fontColor('#FFFFFF')
        .fontWeight(FontWeight.Bold)
        .backgroundColor('#cccccc')
        .height(54)
        .padding(8)
        .borderRadius(8)
        .onClick(() => {
          router.back({
            uri: 'pages/simple/routerApiExample'
          })
        })
    }
    .width('100%')
    .height('100%')
  }
}
// routerApiExample.ets
import router from '@system.router';

@Entry
@Component
struct RouterApiExample {
  build() {
    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
      Text('router路由Api').fontSize(14).fontColor('#CCCCCC').width('90%')
      Text('跳轉到NavigationExample頁面')
        .fontSize(16)
        .fontColor('#FFFFFF')
        .fontWeight(FontWeight.Bold)
        .backgroundColor('#cccccc')
        .height(54)
        .padding(8)
        .borderRadius(8)
        .margin({bottom: 12})
        .onClick(() => {
          router.push({
            uri: 'pages/simple/navigationExample'
          })
        })
      Text('使用NavigationExample頁面替換當前頁')
        .fontSize(16)
        .fontColor('#FFFFFF')
        .fontWeight(FontWeight.Bold)
        .backgroundColor('#cccccc')
        .height(54)
        .padding(8)
        .borderRadius(8)
        .margin({bottom: 12})
        .onClick(() => {
          router.replace({
            uri: 'pages/simple/navigationExample'
          })
        })

      Text('Navigator路由容器元件').fontSize(14).fontColor('#CCCCCC').width('90%')
      Navigator({target: 'pages/simple/navigationExample', type: NavigationType.Back}) {
        Text('返回到NavigationExample頁面')
          .fontSize(16)
          .fontColor('#FFFFFF')
          .fontWeight(FontWeight.Bold)
          .backgroundColor('#cccccc')
          .height(54)
          .padding(8)
          .borderRadius(8)
      }
    }
    .width('100%')
    .height('100%')
  }
}
 

3.7 給標題欄區域按鈕新增頁面跳轉

新建science.ets(科學計算器),housingLoan.ets(房貸計算器),programmer.ets(程式設計師計算器),history.ets(歷史記錄)四個頁面。

  1. 引入routerAPI介面
import router from '@system.router'
  1. 為按鈕新增點選事件
// 在bindMenu選單元素的action中新增路由跳轉
{
  value: "科學",
  action: () => {
    router.push({uri: 'pages/science'})
  }
},
// 給右側歷史記錄按鈕新增onClick事件
.onClick(() => {
  router.push({uri: 'pages/history'})
})
 

總結

本小節對Row容器元件和路由跳轉做了簡單的介紹,下節將繼續完善我們的標準計算器。