HarmonyOS NEXT 實戰(zhàn)之元服務:靜態(tài)案例效果---查看國內航班服務

背景:

前幾篇學習了元服務,后面幾期就讓我們開發(fā)簡單的元服務吧,里面豐富的內容大家自己加痰驱,本期案例 僅供參考

先上本期效果圖 笛质,里面圖片自行替換

效果圖1完整代碼案例如下:

  • Index代碼
import { authentication } from '@kit.AccountKit';import { BusinessError } from '@kit.BasicServicesKit';import { hilog } from '@kit.PerformanceAnalysisKit';import { AddressExchangeViewComponent } from './AddressExchangeViewComponent';export const DATA_CONFIG: Record<string, number> = {  'NUMBER_LEN': 7,  'DURATION_TIME': 200,  'MILLENNIAL_LEN': 3}export const STYLE_CONFIG: Record<string, number> = {  'ITEM_GUTTER': 12,  'ITEM_HEIGHT': 26,  'TEXT_MARGIN': 2,  'PADDING_TOP': 32}@Entry@Componentstruct Index {  build() {    Column({ space: STYLE_CONFIG.ITEM_GUTTER }) {      Text($r('app.string.EntryAbility_label')).fontColor(Color.White)        .fontSize($r('sys.float.ohos_id_text_size_headline8'))        .width($r('app.string.digital_scroll_animation_max_size'))        .textAlign(TextAlign.Center)      AddressExchangeViewComponent()    }    .padding({      top: STYLE_CONFIG.PADDING_TOP    })    .margin({ top: 60 })    .width($r('app.string.digital_scroll_animation_max_size'))    .height($r('app.string.digital_scroll_animation_max_size'))    .linearGradient({      colors: [[$r('app.color.digital_scroll_animation_background_color'), 0.0],        [$r('sys.color.ohos_id_color_background'), 0.3]]    })  }  aboutToAppear() {    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate');    this.loginWithHuaweiID();  }  /**   * Sample code for using HUAWEI ID to log in to atomic service.   * According to the Atomic Service Review Guide, when a atomic service has an account system,   * the option to log in with a HUAWEI ID must be provided.   * The following presets the atomic service to use the HUAWEI ID silent login function.   * To enable the atomic service to log in successfully using the HUAWEI ID, please refer   * to the HarmonyOS HUAWEI ID Access Guide to configure the client ID and fingerprint certificate.   */  private loginWithHuaweiID() {    // Create a login request and set parameters    let loginRequest = new authentication.HuaweiIDProvider().createLoginWithHuaweiIDRequest();    // Whether to forcibly launch the HUAWEI ID login page when the user is not logged in with the HUAWEI ID    loginRequest.forceLogin = false;    // Execute login request    let controller = new authentication.AuthenticationController();    controller.executeRequest(loginRequest).then((data) => {      let loginWithHuaweiIDResponse = data as authentication.LoginWithHuaweiIDResponse;      let authCode = loginWithHuaweiIDResponse.data?.authorizationCode;      // Send authCode to the backend in exchange for unionID, session    }).catch((error: BusinessError) => {      hilog.error(0x0000, 'testTag', 'error: %{public}s', JSON.stringify(error));      if (error.code == authentication.AuthenticationErrorCode.ACCOUNT_NOT_LOGGED_IN) {        // HUAWEI ID is not logged in, it is recommended to jump to the login guide page      }    });  }}
  • AddressExchangeViewComponent 代碼
import curves from '@ohos.curves';import { promptAction } from '@kit.ArkUI';@Preview@Componentexport struct AddressExchangeViewComponent {  @State rotateAngle: number = 0;  @State translateX: number = 0;  @State swap: boolean = false;  private JWidth: number = 300;  build() {    Column({ space: 15 }) {      Row() {        Text($r('app.string.address_exchange_address_left'))          .translate({ x: this.translateX })          .width("40%")          .textAlign(this.swap ? TextAlign.End : TextAlign.Start)          .onClick(() => {            promptAction.showToast({              message: '北京',            });          })        Stack() {          Image($r('app.media.address_exchange_airplane'))            .size({              height: 16,              width: 16            })          Image($r('app.media.address_exchange_recycle'))            .size({              height: 38,              width: 38            })            .rotate({ angle: this.rotateAngle })            .animation({              curve: Curve.EaseOut,              playMode: PlayMode.Normal,            })        }        .width("20%")        .onClick(() => {          this.swap = !this.swap          animateTo({ curve: curves.springMotion() }, () => {            if (this.swap) {              this.translateX = this.JWidth * 0.5;            } else {              this.translateX = 0            }          })          this.rotateAngle += 180;        })        Text('深圳')          .translate({ x: -this.translateX })          .width("40%")          .textAlign(this.swap ? TextAlign.Start : TextAlign.End)          .onClick(() => {            promptAction.showToast({              message: '點了'            });          })      }      .width(this.JWidth)      .borderWidth(1)      .borderRadius(8)      .padding(8)      .height(60)      Row({ space: 15 }) {        Text(this.getDate())          .fontSize($r('app.string.ohos_id_text_size_headline'))          .fontWeight(FontWeight.Medium)          .height(30)        Text($r('app.string.address_exchange_week'))          .height(30)      }      .width(this.JWidth)      .onClick(() => {        promptAction.showToast({          message: '點了'        });      })      Button($r('app.string.address_exchange_search_ticket'))        .fontColor(Color.White)        .height(40)        .backgroundColor('#6486FF')        .width(this.JWidth)        .onClick(() => {          promptAction.showToast({            message: '今日機器出現(xiàn)故障,請找工作室人員解決'          });        })      Row({ space: 10 }) {        Row() {          Text('出差 返現(xiàn)').fontSize(13).fontColor('#222222')          Toggle({ type: ToggleType.Checkbox, isOn: false })        }        Row() {          Row() {            Text('帶兒童/嬰兒').fontSize(13).fontColor('#222222')            Toggle({ type: ToggleType.Checkbox, isOn: true })          }          Row() {            Text('學生票').fontSize(13).fontColor('#222222')            Toggle({ type: ToggleType.Checkbox, isOn: false })          }        }      }.width('100%').justifyContent(FlexAlign.SpaceBetween)      Row() {        Text('每周特價機票')          .fontSize(18)          .fontColor(Color.Black)        Text('查看更多>')          .fontSize(15)          .fontColor(Color.Black)      }.justifyContent(FlexAlign.SpaceBetween).width('100%').padding(10).onClick(() => {      })      Row({ space: 20 }) {        Column({ space: 10 }) {          Text('上海   ->   深圳').fontWeight(FontWeight.Bold).fontSize(13).fontColor('#5A5A5A')          Row() {            Text('12月3日 | 周一').fontSize(10).fontColor('#CAC8C9')            Text('¥600').fontSize(15).fontColor(Color.Red).fontWeight(FontWeight.Bold).margin({ left: 8 })          }.width('100%').justifyContent(FlexAlign.SpaceBetween)          Text() {            Span('原價  ').fontColor('#222222').fontSize(15)            Span('¥1000').fontColor(Color.Red).fontSize(15)          }.fontWeight(FontWeight.Bold).margin({ left: 8 })        }        .width('100%')        .borderRadius(4)        .backgroundColor('#F5F9F8')        .padding(8)        .alignItems(HorizontalAlign.Start)      }      Row({ space: 20 }) {        Column({ space: 10 }) {          Text('北京   ->   天津').fontWeight(FontWeight.Bold).fontSize(13).fontColor('#5A5A5A')          Row() {            Text('12月5日 | 周四').fontSize(10).fontColor('#CAC8C9')            Text('¥300').fontSize(15).fontColor(Color.Red).fontWeight(FontWeight.Bold).margin({ left: 8 })          }.width('100%').justifyContent(FlexAlign.SpaceBetween)          Text() {            Span('原價  ').fontColor('#222222').fontSize(15)            Span('¥400').fontColor(Color.Red).fontSize(15)          }.fontWeight(FontWeight.Bold).margin({ left: 8 })        }        .width('100%')        .borderRadius(4)        .backgroundColor('#F5F9F8')        .padding(8)        .alignItems(HorizontalAlign.Start)      }    }    .width($r('app.string.address_exchange_content_size'))    .height(178)    .margin($r('app.string.ohos_id_card_margin_start'))  }  private getDate() {    const now = new Date();    const year = now.getFullYear();    const month = now.getMonth() + 1; // 注意:月份是從0開始計數(shù)的    const day = now.getDate();    const hours = now.getHours();    const minutes = now.getMinutes();    const seconds = now.getSeconds();    return `${year} 年 ${month} 月 ${day} 日`  }}

最近文章>>>>>>>>>>>

HarmonyOS NEXT實戰(zhàn):元服務與應用 APP 發(fā)布應用市場的詳細步驟與流程

有興趣的同學可以點擊查看源碼


?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌坎藐,老刑警劉巖,帶你破解...
    沈念sama閱讀 216,402評論 6 499
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件哼绑,死亡現(xiàn)場離奇詭異岩馍,居然都是意外死亡,警方通過查閱死者的電腦和手機抖韩,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,377評論 3 392
  • 文/潘曉璐 我一進店門蛀恩,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人茂浮,你說我怎么就攤上這事双谆。” “怎么了席揽?”我有些...
    開封第一講書人閱讀 162,483評論 0 353
  • 文/不壞的土叔 我叫張陵顽馋,是天一觀的道長。 經常有香客問我幌羞,道長寸谜,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,165評論 1 292
  • 正文 為了忘掉前任属桦,我火速辦了婚禮熊痴,結果婚禮上他爸,老公的妹妹穿的比我還像新娘。我一直安慰自己果善,他們只是感情好诊笤,可當我...
    茶點故事閱讀 67,176評論 6 388
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著巾陕,像睡著了一般讨跟。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上惜论,一...
    開封第一講書人閱讀 51,146評論 1 297
  • 那天许赃,我揣著相機與錄音,去河邊找鬼馆类。 笑死,一個胖子當著我的面吹牛弹谁,可吹牛的內容都是我干的乾巧。 我是一名探鬼主播,決...
    沈念sama閱讀 40,032評論 3 417
  • 文/蒼蘭香墨 我猛地睜開眼预愤,長吁一口氣:“原來是場噩夢啊……” “哼沟于!你這毒婦竟也來了?” 一聲冷哼從身側響起植康,我...
    開封第一講書人閱讀 38,896評論 0 274
  • 序言:老撾萬榮一對情侶失蹤旷太,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后销睁,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體供璧,經...
    沈念sama閱讀 45,311評論 1 310
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 37,536評論 2 332
  • 正文 我和宋清朗相戀三年冻记,在試婚紗的時候發(fā)現(xiàn)自己被綠了睡毒。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,696評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡冗栗,死狀恐怖演顾,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情隅居,我是刑警寧澤钠至,帶...
    沈念sama閱讀 35,413評論 5 343
  • 正文 年R本政府宣布,位于F島的核電站胎源,受9級特大地震影響棉钧,放射性物質發(fā)生泄漏。R本人自食惡果不足惜乒融,卻給世界環(huán)境...
    茶點故事閱讀 41,008評論 3 325
  • 文/蒙蒙 一掰盘、第九天 我趴在偏房一處隱蔽的房頂上張望摄悯。 院中可真熱鬧,春花似錦愧捕、人聲如沸奢驯。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,659評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽瘪阁。三九已至,卻和暖如春邮偎,著一層夾襖步出監(jiān)牢的瞬間管跺,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,815評論 1 269
  • 我被黑心中介騙來泰國打工禾进, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留豁跑,地道東北人。 一個月前我還...
    沈念sama閱讀 47,698評論 2 368
  • 正文 我出身青樓泻云,卻偏偏與公主長得像艇拍,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子宠纯,可洞房花燭夜當晚...
    茶點故事閱讀 44,592評論 2 353

推薦閱讀更多精彩內容