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

HarmonyOS實戰(zhàn)—單擊事件的四種寫法

開發(fā) 前端 OpenHarmony
編寫實現(xiàn)類(MyListener)去實現(xiàn) Component.ClickedListener 接口,在類里面重新下 onClick 方法,把點擊代碼實現(xiàn)的操作就寫在 onClick 方法當(dāng)中。

[[414826]]

想了解更多內(nèi)容,請訪問:

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

https://harmonyos.51cto.com

單擊事件的四種寫法  

1. 自己編寫實現(xiàn)類

  • 編寫實現(xiàn)類(MyListener)去實現(xiàn) Component.ClickedListener 接口
  • 在類里面重新下 onClick 方法,把點擊代碼實現(xiàn)的操作就寫在 onClick 方法當(dāng)中
  • 實現(xiàn)代碼:

  • 創(chuàng)建項目名為:ListenerApplication

ability_main.xml

  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.     <Button 
  10.         ohos:id="$+id:but1" 
  11.         ohos:height="match_content" 
  12.         ohos:width="match_content" 
  13.         ohos:text="點我" 
  14.         ohos:text_size="200" 
  15.         ohos:background_element="red"
  16.     </Button> 
  17.  
  18. </DirectionalLayout> 

 MainAbilitySlice

  1. package com.example.listenerapplication.slice; 
  2.  
  3. import com.example.listenerapplication.ResourceTable; 
  4. import ohos.aafwk.ability.AbilitySlice; 
  5. import ohos.aafwk.content.Intent; 
  6. import ohos.agp.components.Button; 
  7. import ohos.agp.components.Component; 
  8.  
  9. public class MainAbilitySlice extends AbilitySlice { 
  10.     @Override 
  11.     public void onStart(Intent intent) { 
  12.         super.onStart(intent); 
  13.         super.setUIContent(ResourceTable.Layout_ability_main); 
  14.  
  15.         //1.找到按鈕 
  16.         //完整寫法:this.findComponentById(ResourceTable.Id_but1); 
  17.         //this:本類的對象,指:MainAbilitySlice(子界面對象) 
  18.         // 在子界面當(dāng)中,通過 id 找到對應(yīng)的組件 
  19.         // 用this去調(diào)用方法,this可以省略不寫 
  20.         //findComponentById(ResourceTable.Id_but1); 
  21.         //返回一個組件對象(所以組件的父類對象) 
  22.         //那么我們在實際寫代碼的時候,需要向下轉(zhuǎn)型:強轉(zhuǎn) 
  23.         Component but1 = (Button) findComponentById(ResourceTable.Id_but1); 
  24.  
  25.         //2.給按鈕綁定單擊事件,當(dāng)點擊后,就會執(zhí)行 MyListener 中的方法,點一次執(zhí)行一次 
  26.         // 而方法就是下面點擊的內(nèi)容 
  27.         but1.setClickedListener(new MyListener()); 
  28.  
  29.     } 
  30.  
  31.     @Override 
  32.     public void onActive() { 
  33.         super.onActive(); 
  34.     } 
  35.  
  36.     @Override 
  37.     public void onForeground(Intent intent) { 
  38.         super.onForeground(intent); 
  39.     } 
  40.  
  41. class MyListener implements Component.ClickedListener{ 
  42.  
  43.     @Override 
  44.     public void onClick(Component component) { 
  45.         //Component:所有組件的父類 
  46.         //component參數(shù): 被點擊的組件對象,在這里就表示按你的對象 
  47.         //component.setText(); setText是子類特有的方法,需要向下轉(zhuǎn)型:強轉(zhuǎn) 
  48.         Button but = (Button) component; 
  49.         but.setText("被點了"); 
  50.     } 

制運行:

  • 點擊后:

2. 當(dāng)前類實現(xiàn)接口

  • ability_main.xml 中把ohos:text_size="50",其他跟上面一樣不變
  • MainAbilitySlice 中只需把上面新建的類 MyListener 給去掉,然后 AbilitySlice 實現(xiàn) ClickedListener 接口類中的 onClick 方法,給本類的 but1按鈕直接綁定單價事件
  1. package com.example.listenerapplication.slice; 
  2.  
  3. import com.example.listenerapplication.ResourceTable; 
  4. import ohos.aafwk.ability.AbilitySlice; 
  5. import ohos.aafwk.content.Intent; 
  6. import ohos.agp.components.Button; 
  7. import ohos.agp.components.Component; 
  8.  
  9. public class MainAbilitySlice extends AbilitySlice implements Component.ClickedListener { 
  10.     @Override 
  11.     public void onStart(Intent intent) { 
  12.         super.onStart(intent); 
  13.         super.setUIContent(ResourceTable.Layout_ability_main); 
  14.  
  15.         //1.找到按鈕 
  16.         //完整寫法:this.findComponentById(ResourceTable.Id_but1); 
  17.         //this:本類的對象,指:MainAbilitySlice(子界面對象) 
  18.         // 在子界面當(dāng)中,通過 id 找到對應(yīng)的組件 
  19.         // 用this去調(diào)用方法,this可以省略不寫 
  20.         //findComponentById(ResourceTable.Id_but1); 
  21.         //返回一個組件對象(所以組件的父類對象) 
  22.         //那么我們在實際寫代碼的時候,需要向下轉(zhuǎn)型:強轉(zhuǎn) 
  23.         Component but1 = (Button) findComponentById(ResourceTable.Id_but1); 
  24.  
  25.         //2.給but1綁定單擊事件,當(dāng)事件被觸發(fā)后,就會執(zhí)行本類中的onClick方法,this就代表本類 
  26.         but1.setClickedListener(this); 
  27.     } 
  28.  
  29.     @Override 
  30.     public void onActive() { 
  31.         super.onActive(); 
  32.     } 
  33.  
  34.     @Override 
  35.     public void onForeground(Intent intent) { 
  36.         super.onForeground(intent); 
  37.     } 
  38.  
  39.     @Override 
  40.     public void onClick(Component component) { 
  41.         Button but = (Button) component; 
  42.         but.setText("被點了——單擊事件的第二種寫法"); 
  43.     } 
  • 運行:

  • 點擊后:

3. 自己編寫實現(xiàn)類 和 當(dāng)前類實現(xiàn)接口 的區(qū)別

如果添加在按鈕上面添加一個Text文本內(nèi)容,當(dāng)按鈕點擊后就會修改文本框的內(nèi)容

改動第一個案例中的代碼:添加Text文本框

  • 上面的 onStart 方法中 text1 是局部變量,如果用第一種方法(自己編寫實現(xiàn)類)來寫, MyListener 不能調(diào)用到 text1 變量

  • 如果使用第二種方法(當(dāng)前類實現(xiàn)接口),就要把 text1 提到成員變量,再把設(shè)置點擊后的內(nèi)容添加到 onClick 方法中

  • 如果在點擊按鈕之后,需要操作其他的組件對象,那么就可以使用第二種方式(當(dāng)前類實現(xiàn)接口)。
  • 如果在點擊按鈕之后,不需要操作其他的組件對象,就可以使用第一種方式(自己編寫實現(xiàn)類)。

4. 匿名內(nèi)部類

采用匿名內(nèi)部類就不需要實現(xiàn) implement ClickedListener 接口,也不需要再新建一個類了

  • 但使用匿名內(nèi)部類的代碼只能使用一次。當(dāng)使用代碼需要用一次的時候,可以采用匿名內(nèi)部類的形式來簡化代碼
  • 直接 new ClickedListener 就能實現(xiàn)了,然后把第一種實現(xiàn)方式(自己編寫實現(xiàn)類)中的 onClick 拿過來或第二種方式(當(dāng)前類實現(xiàn)接口)實現(xiàn)的 onClick 方法拿過來就行了(其實這兩者的onClick方法的內(nèi)容是一樣的),如下:
  1. but1.setClickedListener(new Component.ClickedListener() { 
  2.     @Override 
  3.     public void onClick(Component component) { 
  4.         Button but = (Button) component; 
  5.         but.setText("被點了——單擊事件的第三種寫法"); 
  6.         text1.setText("被點擊了"); 
  7.     } 
  8. }); 

 運行:

  • 當(dāng)被點擊后,觸發(fā)了 onClick 方法中兩個設(shè)置文本的方法(Button和Text文本都發(fā)生了變化)

5. 方法引用

  • 這個方法的形參和方法的返回值類型需要跟接口里的抽象方法里的形參和返回值類型要保持一致
  • 代碼實現(xiàn),布局代碼不變跟匿名內(nèi)部類的一致,改動如下:
  • 直接編寫 onClick 方法,不帶 @Override ,然后在 onStart 方法中直接調(diào)用即可
  1. package com.example.listenerapplication.slice; 
  2.  
  3. import com.example.listenerapplication.ResourceTable; 
  4. import ohos.aafwk.ability.AbilitySlice; 
  5. import ohos.aafwk.content.Intent; 
  6. import ohos.agp.components.Button; 
  7. import ohos.agp.components.Component; 
  8. import ohos.agp.components.Text; 
  9.  
  10. public class MainAbilitySlice extends AbilitySlice { 
  11.     Text text1 = null
  12.     @Override 
  13.     public void onStart(Intent intent) { 
  14.         super.onStart(intent); 
  15.         super.setUIContent(ResourceTable.Layout_ability_main); 
  16.  
  17.         Component but1 = (Button) findComponentById(ResourceTable.Id_but1); 
  18.  
  19.         text1 = (Text) findComponentById(ResourceTable.Id_text1); 
  20.  
  21.         but1.setClickedListener(this::onClick); 
  22.     } 
  23.  
  24.     @Override 
  25.     public void onActive() { 
  26.         super.onActive(); 
  27.     } 
  28.  
  29.     @Override 
  30.     public void onForeground(Intent intent) { 
  31.         super.onForeground(intent); 
  32.     } 
  33.  
  34.  
  35.     public void onClick(Component component) { 
  36.         Button but = (Button) component; 
  37.         but.setText("被點了——單擊事件的第四種寫法"); 
  38.         text1.setText("被點擊了"); 
  39.     } 

  • 當(dāng)按鈕被點擊后,就要執(zhí)行this本類中的onClick方法,相當(dāng)于把下面的public void onClick...方法拿過來,引用了一下,當(dāng)做抽象方法的方法體。
  • 運行:

6. 小節(jié)

當(dāng)前類作為實現(xiàn)類和方法引用是比較常用的。其他的寫法也要掌握了解即可。

想了解更多內(nèi)容,請訪問:

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

https://harmonyos.51cto.com

 

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

2022-06-10 08:01:17

ReduxReact

2022-03-22 08:59:32

勒索軟件網(wǎng)絡(luò)安全網(wǎng)絡(luò)攻擊

2011-11-24 16:34:39

Java

2011-03-16 09:05:53

NATiptables

2012-09-11 09:55:26

編程HTML5編程能力

2017-07-06 15:40:19

DevOps核心能力

2019-10-24 07:42:28

Java引用GC

2021-10-24 08:37:18

網(wǎng)絡(luò)監(jiān)控網(wǎng)絡(luò)架構(gòu)網(wǎng)絡(luò)

2013-06-14 15:24:57

Android開發(fā)移動開發(fā)數(shù)據(jù)存儲方式

2013-05-30 10:06:05

光纖光纖跳線

2018-12-05 16:25:14

2020-06-12 08:28:29

JavaScript開發(fā)技術(shù)

2020-11-10 10:08:41

Kubernetes容器開發(fā)

2021-12-01 23:05:27

物聯(lián)網(wǎng)計算數(shù)據(jù)

2010-10-19 17:40:30

SqlServer主鍵

2023-02-10 11:13:42

網(wǎng)絡(luò)功耗無線網(wǎng)絡(luò)設(shè)備

2010-08-19 11:16:30

虛擬化

2021-08-11 20:17:22

推薦算法系統(tǒng)

2010-07-28 13:54:42

Flex數(shù)據(jù)綁定

2011-06-01 17:35:35

Android Activity
點贊
收藏

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