自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

基于HarmonyOS ArkUI 3.0框架,我成功開發(fā)了流式布局網(wǎng)絡(luò)語

開發(fā) OpenHarmony
簡(jiǎn)單介紹一下本文的實(shí)現(xiàn)效果,在文本輸入框輸入流行網(wǎng)絡(luò)用語,點(diǎn)擊新增按鈕,把流行網(wǎng)絡(luò)用語添加到下面流式布局的組件里。

[[432515]]

想了解更多內(nèi)容,請(qǐng)?jiān)L問:

51CTO和華為官方合作共建的鴻蒙技術(shù)社區(qū)

https://harmonyos.51cto.com

一、前言

在介紹之前,吐槽一下為什么文檔Ets組件里沒有文本輸入框,這么基礎(chǔ)的組件都不先提供的,好的當(dāng)時(shí)在HDC大會(huì)體驗(yàn)Ets實(shí)例時(shí),見到有用輸入框,名叫TextInput,在開發(fā)工具是沒有提示的,然后我也嘗試用它來為我的Demo提供輸入,然而發(fā)現(xiàn)個(gè)問題,當(dāng)我把TextInput組件里的text屬于綁定@State 定義變量時(shí),每次在文本框輸入內(nèi)容,點(diǎn)擊按鈕,清空變量值,這時(shí)文本輸入框也清空了,但當(dāng)我再次輸入內(nèi)容時(shí),輸入文本框會(huì)追加上次的內(nèi)容,估計(jì)是因?yàn)門extInput有這個(gè)Bug,所以還沒有在文檔上顯示出來。

簡(jiǎn)單介紹一下本文的實(shí)現(xiàn)效果,在文本輸入框輸入流行網(wǎng)絡(luò)用語,點(diǎn)擊新增按鈕,把流行網(wǎng)絡(luò)用語添加到下面流式布局的組件里,文以x、y軸縮放從0.5變化到1,透明度從0到1 顯示出來,點(diǎn)擊隨機(jī)刪除按鈕時(shí),從下面流行網(wǎng)絡(luò)用語中隨機(jī)刪除一個(gè),并且以沿y軸旋轉(zhuǎn)360度消失。

二、實(shí)現(xiàn)效果

基于HarmonyOS ArkUI 3.0 框架,我成功開發(fā)了流式布局網(wǎng)絡(luò)語-鴻蒙HarmonyOS技術(shù)社區(qū)
基于HarmonyOS ArkUI 3.0 框架,我成功開發(fā)了流式布局網(wǎng)絡(luò)語-鴻蒙HarmonyOS技術(shù)社區(qū)

三、創(chuàng)建工程

在這當(dāng)作你已經(jīng)安裝好最新版本DevEco-Studio開發(fā)工具, 點(diǎn)擊File -> New -> New Project… 彈出Create HarmonyOS Project窗口, 這里我選擇空白eTS模板創(chuàng)建, 下來就跟著一起玩轉(zhuǎn)HarmonyOS ArkUI 3.0 框架聲明式開發(fā)吧.

基于HarmonyOS ArkUI 3.0 框架,我成功開發(fā)了流式布局網(wǎng)絡(luò)語-鴻蒙HarmonyOS技術(shù)社區(qū)
基于HarmonyOS ArkUI 3.0 框架,我成功開發(fā)了流式布局網(wǎng)絡(luò)語-鴻蒙HarmonyOS技術(shù)社區(qū)

四、界面開發(fā)

界面有三個(gè)組件組合而成,文本輸入框和新增按鈕組合成一個(gè)自定義組件, 歷史記錄和隨機(jī)刪除按鈕組合成一個(gè)自定義組件,滾動(dòng)組件和多個(gè)文本組件組合成一個(gè)自定義組件,下面我們分別從上到下來介紹自定義組件:

  1. @Entry 
  2. @Component 
  3. struct Search_FlowLayout { 
  4.   @State historyArr:string[] = ["人生如戲""養(yǎng)豬才洋氣""忍為高""牢房不倒我不學(xué)好""你帶著錢""我消極對(duì)待減肥""寧可高傲的發(fā)霉""接下來的不是故事就是事故""腳踏實(shí)地的干""可是用錢卻買不到""拍腦袋決策""笑完我就去睡覺""有錢的都是大爺","但是欠錢不還的更是"
  5.  
  6.   build() { 
  7.     Column() { 
  8.       // 文本輸入框和新增按鈕組合自定義組件 
  9.       Search_Input({historyArr: $historyArr}) 
  10.       // 歷史記錄和隨機(jī)刪除按鈕組合成自定義組件 
  11.       Clear_History({historyArr: $historyArr}) 
  12.       // 滾動(dòng)組件和多個(gè)文本組件組合成自定義組件 
  13.       Flowlayout_Container({historyArr: $historyArr}) 
  14.     } 
  15.     .alignItems(HorizontalAlign.Center) 
  16.   } 

 實(shí)現(xiàn)組件內(nèi)轉(zhuǎn)場(chǎng)動(dòng)效,通過點(diǎn)擊新增按鈕或隨機(jī)刪除按鈕來控制Text組件的添加和移除,呈現(xiàn)容器組件子組件添加和移除時(shí)的動(dòng)效。

這里用到組件轉(zhuǎn)場(chǎng)動(dòng)畫,簡(jiǎn)單說一下組件轉(zhuǎn)場(chǎng)主要通過transition屬性方法配置轉(zhuǎn)場(chǎng)參數(shù),在組件添加和移除時(shí)會(huì)執(zhí)行過渡動(dòng)效,需要配合animteTo才能生效。動(dòng)效時(shí)長(zhǎng)、曲線、延時(shí)跟隨animateTo中的配置。

文本輸入框和新增按鈕組合,在新增按鈕的onClick事件中添加animateTo方法,來使下面Text子組件動(dòng)效生效。

  1. @Component 
  2. struct Search_Input { 
  3.   // 存儲(chǔ)文本輸入框內(nèi)容 
  4.   @State historyInput: string = '' 
  5.   // 鏈接主入口歷史記錄數(shù)組 
  6.   @Link historyArr: string[] 
  7.  
  8.   build() { 
  9.     // Flex布局,居中顯示 
  10.     Flex({ alignItems: ItemAlign.Center }){ 
  11.       // 文本輸入框 
  12.       TextInput({ placeholder: '請(qǐng)輸入...', text: this.historyInput }) 
  13.         .type(InputType.Normal) 
  14.         .placeholderColor(Color.Gray) 
  15.         .placeholderFont({ size: 50, weight: 2}) 
  16.         .enterKeyType(EnterKeyType.Search) 
  17.         .caretColor(Color.Green) 
  18.         .layoutWeight(8) 
  19.         .height(40) 
  20.         .borderRadius('20px'
  21.         .backgroundColor(Color.White) 
  22.         .onChange((value: string) => { 
  23.           this.historyInput = value 
  24.         }) 
  25.  
  26.       // 新增網(wǎng)絡(luò)用語按鈕 
  27.       Button({type: ButtonType.Capsule, stateEffect:false}) { 
  28.         Text('新增').fontSize(17).fontColor(Color.Blue) 
  29.       } 
  30.       .layoutWeight(2) 
  31.       .backgroundColor('#00000000'
  32.       .onClick((event: ClickEvent) => { 
  33.         if (this.historyInput != null && this.historyInput.length > 0) { 
  34.           // 添加animateTo方法,來使Text組件子組件動(dòng)效生效 
  35.           animateTo({ duration: 600 }, () => { 
  36.             this.historyArr.unshift(this.historyInput) 
  37.           }) 
  38.         } 
  39.         this.historyInput = '' 
  40.       }) 
  41.     } 
  42.     .height(60) 
  43.     .padding({left: 10}) 
  44.     .backgroundColor('#FFedf2f5'
  45.   } 

歷史記錄和隨機(jī)刪除按鈕組合

  1. @Component 
  2. struct Clear_History { 
  3.   // 鏈接主入口歷史記錄數(shù)組 
  4.   @Link historyArr: string[] 
  5.  
  6.   build() { 
  7.     Flex({ alignItems: ItemAlign.Center }) { 
  8.       if (this.historyArr.length > 0) { 
  9.         Text('歷史記錄'
  10.           .fontSize(14) 
  11.           .fontColor(Color.Grey) 
  12.           .layoutWeight(5) 
  13.  
  14.         Text('隨機(jī)刪除'
  15.           .textAlign(TextAlign.End
  16.           .margin({right: 30}) 
  17.           .fontSize(14) 
  18.           .fontColor(Color.Red) 
  19.           .layoutWeight(5) 
  20.           .onClick((event: ClickEvent) => { 
  21.             // 添加animateTo方法,來使Text組件子組件動(dòng)效生效 
  22.             // 隨機(jī)從歷史記錄刪除一個(gè) 
  23.             animateTo({ duration: 600 }, () => { 
  24.               var idx = Math.floor(Math.random()*this.historyArr.length); 
  25.               this.historyArr.splice(idx, 1) 
  26.             }) 
  27.           }) 
  28.       } 
  29.     } 
  30.     .height(40) 
  31.     .padding({ left: 20, top: 10 }) 
  32.   } 

滾動(dòng)組件和多個(gè)文本組件組合成, 給Text組件添加兩個(gè)transition屬性,分別用于定義組件的添加動(dòng)效和移除動(dòng)效。

  1. @Component 
  2. struct Flowlayout_Container { 
  3.   // 鏈接主入口歷史記錄數(shù)組 
  4.   @Link historyArr: string[] 
  5.  
  6.   build() { 
  7.     // 滾動(dòng)組件 
  8.     Scroll() { 
  9.       // Flex布局, wrap為FlexWrap.Wrap為流式布局 
  10.       Flex({justifyContent: FlexAlign.Start, wrap: FlexWrap.Wrap}) { 
  11.         if (this.historyArr.length > 0) { 
  12.           // 循環(huán)顯示出網(wǎng)絡(luò)用語到文本組件 
  13.           ForEach(this.historyArr, 
  14.             (item:string) => { 
  15.               Text(`${item}`) 
  16.                 .fontSize(18) 
  17.                 .borderStyle(BorderStyle.Solid) 
  18.                 .borderWidth('1px'
  19.                 .borderColor('#dddddd'
  20.                 .borderRadius('90px'
  21.                 .padding({top: 4, bottom: 4, right: 10, left: 10}) 
  22.                 .margin({top: 10, right: 10}) 
  23.                 .textOverflow({overflow: TextOverflow.Ellipsis}) 
  24.                 .maxLines(2) 
  25.                 // 新增網(wǎng)絡(luò)用語時(shí)的動(dòng)畫 
  26.                 .transition({ type: TransitionType.Insert, scale: { x: 0.5, y: 0.5 }, opacity: 0 }) 
  27.                 // 隨機(jī)刪除網(wǎng)絡(luò)用語的動(dòng)畫 
  28.                 .transition({ type: TransitionType.Delete, rotate: { x: 0, y: 1, z: 0, angle: 360 }, scale: { x: 0, y: 0 } }) 
  29.             }, 
  30.             (item:string) => item.toString() 
  31.           ) 
  32.         } 
  33.       } 
  34.       .margin({left: 20, right: 10, bottom: 100}) 
  35.       .padding({bottom: 10}) 
  36.     } 
  37.   } 

介紹就到此了,聲明式開發(fā),是不是簡(jiǎn)潔了很多,大家一起擼起來吧。

想了解更多內(nèi)容,請(qǐng)?jiān)L問:

51CTO和華為官方合作共建的鴻蒙技術(shù)社區(qū)

https://harmonyos.51cto.com

 

責(zé)任編輯:jianghua 來源: 鴻蒙社區(qū)
相關(guān)推薦

2021-12-01 15:38:33

鴻蒙HarmonyOS應(yīng)用

2021-12-01 15:40:23

鴻蒙HarmonyOS應(yīng)用

2021-11-02 14:52:17

鴻蒙HarmonyOS應(yīng)用

2023-02-13 00:18:22

前端庫(kù)框架集合

2021-10-28 14:58:15

鴻蒙HarmonyOS應(yīng)用

2022-11-10 09:28:40

框架開發(fā)

2015-06-24 10:17:24

UI流式布局

2022-06-27 14:12:32

css鴻蒙自定義

2021-12-03 09:49:59

鴻蒙HarmonyOS應(yīng)用

2024-01-11 15:54:55

eTS語言TypeScript應(yīng)用開發(fā)

2022-03-17 15:28:18

五子棋HarmonyOSJSAPI

2023-04-12 07:35:33

2021-12-27 15:10:55

鴻蒙HarmonyOS應(yīng)用

2021-04-20 09:42:20

鴻蒙HarmonyOS應(yīng)用開發(fā)

2024-05-31 08:43:31

2022-11-02 16:06:54

ArkUIETS

2022-10-24 14:49:54

ArkUI心電圖組件

2023-08-17 15:01:08

ArkUI布局渲染

2018-09-03 15:23:58

2014-12-17 10:12:06

HybridAppFramewor頁(yè)面布局
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)