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

HarmonyOS實(shí)戰(zhàn)—實(shí)現(xiàn)相親APP

系統(tǒng) OpenHarmony
本篇帶給大家運(yùn)用HarmonyOS打造一款相親APP,愿社區(qū)的每一位單身的同志都能通過逛社區(qū)找到心儀的對象。

[[419696]]

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

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

https://harmonyos.51cto.com

簡易版 相親APP

實(shí)現(xiàn)如下效果:

如果喜歡就點(diǎn)擊下面的“聯(lián)系方式”,不喜歡就點(diǎn)擊“下一個(gè)”

HarmonyOS實(shí)戰(zhàn)—實(shí)現(xiàn)相親APP-鴻蒙HarmonyOS技術(shù)社區(qū)
HarmonyOS實(shí)戰(zhàn)—實(shí)現(xiàn)相親APP-鴻蒙HarmonyOS技術(shù)社區(qū)

1. 布局實(shí)現(xiàn)

新建項(xiàng)目:MakeFriendsApplication

把以下九個(gè)girl圖片復(fù)制到 media 文件夾下

HarmonyOS實(shí)戰(zhàn)—實(shí)現(xiàn)相親APP-鴻蒙HarmonyOS技術(shù)社區(qū)
HarmonyOS實(shí)戰(zhàn)—實(shí)現(xiàn)相親APP-鴻蒙HarmonyOS技術(shù)社區(qū)

有需要上面圖片素材的小伙伴可以自?。篽ttps://www.aliyundrive.com/s/j59dy5redPR

當(dāng)然也可以自己在網(wǎng)上找,圖片信息如下:

HarmonyOS實(shí)戰(zhàn)—實(shí)現(xiàn)相親APP-鴻蒙HarmonyOS技術(shù)社區(qū)

可以在上面的實(shí)現(xiàn)看到布局是由:一個(gè)圖片+三個(gè)文本+兩個(gè)按鈕 組成的

布局實(shí)現(xiàn):

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <DirectionalLayout 
  3.     xmlns:ohos="http://schemas.huawei.com/res/ohos" 
  4.     ohos:height="match_parent" 
  5.     ohos:width="match_parent" 
  6.     ohos:alignment="center" 
  7.     ohos:orientation="vertical"
  8.  
  9.     <Image 
  10.         ohos:id="$+id:img" 
  11.         ohos:height="match_content" 
  12.         ohos:width="match_content" 
  13.         ohos:image_src="$media:girl1"/> 
  14.  
  15.     <Text 
  16.         ohos:id="$+id:name" 
  17.         ohos:height="50vp" 
  18.         ohos:width="150vp" 
  19.         ohos:text="姓名:王美花" 
  20.         ohos:text_size="20fp"/> 
  21.  
  22.     <Text 
  23.         ohos:id="$+id:age" 
  24.         ohos:height="50vp" 
  25.         ohos:width="150vp" 
  26.         ohos:text="年齡:29" 
  27.         ohos:text_size="20fp"/>  
  28.  
  29.     <Text 
  30.         ohos:id="$+id:address" 
  31.         ohos:height="50vp" 
  32.         ohos:width="150vp" 
  33.         ohos:text="地址:南京" 
  34.         ohos:text_size="20fp"/> 
  35.      
  36.     <Button 
  37.         ohos:id="$+id:next" 
  38.         ohos:height="50vp" 
  39.         ohos:width="150vp" 
  40.         ohos:background_element="#92D050" 
  41.         ohos:text="下一個(gè)" 
  42.         ohos:text_size="20fp" 
  43.         ohos:text_color="#FFFFFF"/> 
  44.  
  45.     <Button 
  46.         ohos:id="$+id:get" 
  47.         ohos:height="50vp" 
  48.         ohos:width="150vp" 
  49.         ohos:background_element="#92D050" 
  50.         ohos:text="獲取聯(lián)系方式" 
  51.         ohos:text_size="20fp" 
  52.         ohos:text_color="#FFFFFF" 
  53.         ohos:top_margin="10vp"/> 
  54.  
  55. </DirectionalLayout> 

運(yùn)行:

HarmonyOS實(shí)戰(zhàn)—實(shí)現(xiàn)相親APP-鴻蒙HarmonyOS技術(shù)社區(qū)

2. 核心業(yè)務(wù)邏輯實(shí)現(xiàn)

先找到組件對象

HarmonyOS實(shí)戰(zhàn)—實(shí)現(xiàn)相親APP-鴻蒙HarmonyOS技術(shù)社區(qū)

業(yè)務(wù):點(diǎn)擊了按鈕“下一個(gè)”就要修改上面的信息,能不能把上面的信息看作成一個(gè)整體的對象?

現(xiàn)在有9個(gè)相親對象,點(diǎn)擊“下一個(gè)”的時(shí)候,就會(huì)隨機(jī)把一個(gè)相親對象的信息去顯示在上面。

創(chuàng)建一個(gè)Javabean類去描述女朋友信息,有了Javabean類后才能在代碼中創(chuàng)建對象

HarmonyOS實(shí)戰(zhàn)—實(shí)現(xiàn)相親APP-鴻蒙HarmonyOS技術(shù)社區(qū)

在domain中新建類名為:GirlFriend,在類中的屬性有哪些?

如:圖片、姓名、地址、年齡都是GirlFriend類中的屬性

在資源管理器中獲取圖片,發(fā)現(xiàn)girl圖片是int類型的

HarmonyOS實(shí)戰(zhàn)—實(shí)現(xiàn)相親APP-鴻蒙HarmonyOS技術(shù)社區(qū)

所以在JavaBean中GirlFriend類中圖片類型使用int類型

HarmonyOS實(shí)戰(zhàn)—實(shí)現(xiàn)相親APP-鴻蒙HarmonyOS技術(shù)社區(qū)

其他屬性使用對應(yīng)的類型

HarmonyOS實(shí)戰(zhàn)—實(shí)現(xiàn)相親APP-鴻蒙HarmonyOS技術(shù)社區(qū)

最后生成標(biāo)準(zhǔn)的JavaBean即可

  1. package com.xdr630.makefriendsapplication.domain; 
  2.  
  3. public class GirlFriend { 
  4.     //照片 
  5.     private int photoID; 
  6.     //姓名 
  7.     private String name
  8.     //年齡 
  9.     private int age; 
  10.     //地址 
  11.     private String address; 
  12.  
  13.     //空參 + 全參:alt + insert 
  14.  
  15.     public GirlFriend() { 
  16.     } 
  17.  
  18.     public GirlFriend(int photoID, String nameint age, String address) { 
  19.         this.photoID = photoID; 
  20.         this.name = name
  21.         this.age = age; 
  22.         this.address = address; 
  23.     } 
  24.  
  25.     public int getPhotoID() { 
  26.         return photoID; 
  27.     } 
  28.  
  29.     public void setPhotoID(int photoID) { 
  30.         this.photoID = photoID; 
  31.     } 
  32.  
  33.     public String getName() { 
  34.         return name
  35.     } 
  36.  
  37.     public void setName(String name) { 
  38.         this.name = name
  39.     } 
  40.  
  41.     public int getAge() { 
  42.         return age; 
  43.     } 
  44.  
  45.     public void setAge(int age) { 
  46.         this.age = age; 
  47.     } 
  48.  
  49.     public String getAddress() { 
  50.         return address; 
  51.     } 
  52.  
  53.     public void setAddress(String address) { 
  54.         this.address = address; 
  55.     } 

創(chuàng)建好了Javabean,就可以創(chuàng)建一個(gè)集合裝9個(gè)相親的對象,使用集合來存儲(chǔ)了,ArrayList,泛型就可以直接寫創(chuàng)建好的GirlFriend 類

HarmonyOS實(shí)戰(zhàn)—實(shí)現(xiàn)相親APP-鴻蒙HarmonyOS技術(shù)社區(qū)

在以后學(xué)習(xí)了和服務(wù)器交互之后,這些數(shù)據(jù)都是從服務(wù)器獲取的,就不是一個(gè)一個(gè)添加了

下面就是手動(dòng)添加數(shù)據(jù)來源

創(chuàng)建一個(gè)女朋友對象(new GirlFriend )并添加到 list 當(dāng)中

為了方便,省的去查看Javabean中屬性的排序,Ctrl + P 就可以查看參數(shù)了

HarmonyOS實(shí)戰(zhàn)—實(shí)現(xiàn)相親APP-鴻蒙HarmonyOS技術(shù)社區(qū)

添加完成9個(gè)對象

HarmonyOS實(shí)戰(zhàn)—實(shí)現(xiàn)相親APP-鴻蒙HarmonyOS技術(shù)社區(qū)

接著給按鈕添加點(diǎn)擊事件,兩個(gè)按鈕都要添加,分別是“next”和“get”按鈕

實(shí)現(xiàn)本類中點(diǎn)擊事件

HarmonyOS實(shí)戰(zhàn)—實(shí)現(xiàn)相親APP-鴻蒙HarmonyOS技術(shù)社區(qū)

當(dāng)next按鈕被點(diǎn)擊后,就會(huì)執(zhí)行本類中的onClick方法

HarmonyOS實(shí)戰(zhàn)—實(shí)現(xiàn)相親APP-鴻蒙HarmonyOS技術(shù)社區(qū)

get 按鈕同上也要添加單擊事件

HarmonyOS實(shí)戰(zhàn)—實(shí)現(xiàn)相親APP-鴻蒙HarmonyOS技術(shù)社區(qū)

然后在 onClick 方法中判斷點(diǎn)擊是 next 按鈕 或 get 按鈕

因?yàn)樵?onClick 方法中要用到按鈕的對象,所以把按鈕的對象提升為 成員變量 才能在onClick方法中被訪問的到,用到哪個(gè)就提哪個(gè),這些組件都用的到,所以都提為成員變量。

在下面onClick方法當(dāng)中,要從集合中獲取girl信息,所以集合也要提為成員變量

HarmonyOS實(shí)戰(zhàn)—實(shí)現(xiàn)相親APP-鴻蒙HarmonyOS技術(shù)社區(qū)

Random對象也放在成員位置,表示在程序啟動(dòng)之后就創(chuàng)建了一個(gè),如果放在onClick方法中,當(dāng)點(diǎn)擊一次就創(chuàng)建一次,內(nèi)存冗余。

HarmonyOS實(shí)戰(zhàn)—實(shí)現(xiàn)相親APP-鴻蒙HarmonyOS技術(shù)社區(qū)

MainAbilitySlice

  1. package com.xdr630.makefriendsapplication.slice; 
  2.  
  3. import com.xdr630.makefriendsapplication.ResourceTable; 
  4. import com.xdr630.makefriendsapplication.domain.GirlFriend; 
  5. import ohos.aafwk.ability.AbilitySlice; 
  6. import ohos.aafwk.content.Intent; 
  7. import ohos.agp.components.Button; 
  8. import ohos.agp.components.Component; 
  9. import ohos.agp.components.Image; 
  10. import ohos.agp.components.Text; 
  11.  
  12. import java.util.ArrayList; 
  13. import java.util.Random; 
  14.  
  15. public class MainAbilitySlice extends AbilitySlice implements Component.ClickedListener { 
  16.  
  17.         Image img; 
  18.         Text name
  19.         Text age; 
  20.         Text address; 
  21.         Button next
  22.         Button get; 
  23.  
  24.         ArrayList<GirlFriend> list = new ArrayList<>(); 
  25.  
  26.     @Override 
  27.     public void onStart(Intent intent) { 
  28.         super.onStart(intent); 
  29.         super.setUIContent(ResourceTable.Layout_ability_main); 
  30.  
  31.         //1.找到組件對象 
  32.         img = (Image) findComponentById(ResourceTable.Id_img); 
  33.         name = (Text) findComponentById(ResourceTable.Id_name); 
  34.         age = (Text) findComponentById(ResourceTable.Id_age); 
  35.         address = (Text) findComponentById(ResourceTable.Id_address); 
  36.         next = (Button) findComponentById(ResourceTable.Id_next); 
  37.         get = (Button) findComponentById(ResourceTable.Id_get); 
  38.         //2.創(chuàng)建一個(gè)集合裝9個(gè)相親的對象 
  39.  
  40.  
  41.         //添加9個(gè)對象 
  42.         //在以后學(xué)習(xí)了和服務(wù)器交互之后,這些數(shù)據(jù)都是從服務(wù)器獲取的 
  43.         list.add(new GirlFriend(ResourceTable.Media_girl1,"王美花1",29,"南京")); 
  44.         list.add(new GirlFriend(ResourceTable.Media_girl2,"王美花2",30,"上海")); 
  45.         list.add(new GirlFriend(ResourceTable.Media_girl3,"王美花3",31,"武漢")); 
  46.         list.add(new GirlFriend(ResourceTable.Media_girl4,"王美花4",28,"長沙")); 
  47.         list.add(new GirlFriend(ResourceTable.Media_girl5,"王美花5",25,"南昌")); 
  48.         list.add(new GirlFriend(ResourceTable.Media_girl6,"王美花6",26,"杭州")); 
  49.         list.add(new GirlFriend(ResourceTable.Media_girl7,"王美花7",23,"深圳")); 
  50.         list.add(new GirlFriend(ResourceTable.Media_girl8,"王美花8",22,"北京")); 
  51.         list.add(new GirlFriend(ResourceTable.Media_girl9,"王美花9",24,"廣州")); 
  52.  
  53.         //3.給按鈕添加點(diǎn)擊事件 
  54.         next.setClickedListener(this); 
  55.         get.setClickedListener(this); 
  56.  
  57.     } 
  58.  
  59.     @Override 
  60.     public void onActive() { 
  61.         super.onActive(); 
  62.     } 
  63.  
  64.     @Override 
  65.     public void onForeground(Intent intent) { 
  66.         super.onForeground(intent); 
  67.     } 
  68.  
  69.     Random r =  new Random(); 
  70.  
  71.     @Override 
  72.     public void onClick(Component component) { 
  73.         if (component == next){ 
  74.             //點(diǎn)擊的是下一個(gè) --- 換一個(gè)girl的信息 
  75.             //從集合中獲取一個(gè)隨機(jī)的girl的信息 
  76.  
  77.             //獲取一個(gè)隨機(jī)索引 
  78.             int randomIndex = r.nextInt(list.size()); 
  79.             //通過隨機(jī)索引獲取隨機(jī)girl信息 
  80.             GirlFriend girlFriend = list.get(randomIndex); 
  81.             //把隨機(jī)出來的信息設(shè)置到界面當(dāng)中 
  82.             img.setImageAndDecodeBounds(girlFriend.getPhotoID()); 
  83.             name.setText("姓名:" + girlFriend.getName()); 
  84.             age.setText("年齡:" + girlFriend.getAge()); 
  85.             address.setText("地址:" + girlFriend.getAddress()); 
  86.  
  87.  
  88.         }else if (component == get){ 
  89.             //獲取聯(lián)系方式 
  90.             //擴(kuò)展:可以跳轉(zhuǎn)到界面讓用戶充值界面,充值后才能看到girl信息 
  91.         } 
  92.     } 

運(yùn)行:

HarmonyOS實(shí)戰(zhàn)—實(shí)現(xiàn)相親APP-鴻蒙HarmonyOS技術(shù)社區(qū)

點(diǎn)擊“下一個(gè)”

HarmonyOS實(shí)戰(zhàn)—實(shí)現(xiàn)相親APP-鴻蒙HarmonyOS技術(shù)社區(qū)
HarmonyOS實(shí)戰(zhàn)—實(shí)現(xiàn)相親APP-鴻蒙HarmonyOS技術(shù)社區(qū)

當(dāng)點(diǎn)擊“下一個(gè)”按鈕時(shí),信息也會(huì)隨之一起獲取的到,所以有需求的小伙伴可以擴(kuò)展下,當(dāng)點(diǎn)擊“下一個(gè)”按鈕時(shí)信息是隱藏的,只有當(dāng)點(diǎn)擊“獲取聯(lián)系方式”時(shí)才能顯示出對應(yīng)的信息,或也可以添加充值頁面,當(dāng)點(diǎn)擊“獲取聯(lián)系方式”時(shí),就跳轉(zhuǎn)到充值頁面,只有充值完后才能看到對應(yīng)的信息。

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

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

https://harmonyos.51cto.com

 

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

2015-09-18 10:44:54

大數(shù)據(jù)相親

2022-09-01 11:04:47

PythonApp

2010-12-10 11:26:56

“相親式”招聘

2010-07-26 15:30:43

面試

2015-05-30 10:04:24

線下公開課51CTO沙龍MDSA

2024-07-26 16:17:22

2014-12-17 10:29:59

混合應(yīng)用Hybrid App開發(fā)實(shí)戰(zhàn)

2020-11-05 10:05:25

App

2021-01-14 09:50:26

鴻蒙HarmonyOSAPP

2021-07-05 14:29:28

鴻蒙HarmonyOS應(yīng)用

2010-06-24 10:59:56

APP-V虛擬化應(yīng)用虛擬化

2010-06-23 10:09:31

APP-V虛擬化應(yīng)用虛擬化

2013-03-19 10:33:48

大數(shù)據(jù)相親數(shù)據(jù)分析

2020-08-25 15:17:12

戴爾

2009-01-08 11:05:55

學(xué)賽 IT教育

2019-01-22 15:26:48

APP會(huì)員自動(dòng)續(xù)費(fèi)簽約

2021-08-10 15:37:34

鴻蒙HarmonyOS應(yīng)用

2018-03-30 10:28:23

2020-08-27 08:18:39

機(jī)器學(xué)習(xí)

2024-02-19 15:40:37

鴻蒙App備案阿里云
點(diǎn)贊
收藏

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