日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
HarmonyOS實戰(zhàn)—單擊事件的四種寫法

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

網(wǎng)站建設(shè)哪家好,找成都創(chuàng)新互聯(lián)!專注于網(wǎng)頁設(shè)計、網(wǎng)站建設(shè)、微信開發(fā)、重慶小程序開發(fā)、集團(tuán)企業(yè)網(wǎng)站建設(shè)等服務(wù)項目。為回饋新老客戶創(chuàng)新互聯(lián)還提供了廣水免費建站歡迎大家使用!

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

https://harmonyos.

單擊事件的四種寫法

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.  
  2.     xmlns:ohos="http://schemas.huawei.com/res/ohos" 
  3.     ohos:height="match_parent" 
  4.     ohos:width="match_parent" 
  5.     ohos:alignment="center" 
  6.     ohos:orientation="vertical"> 
  7.  
  8.     
  9.         ohos:id="$+id:but1" 
  10.         ohos:height="match_content" 
  11.         ohos:width="match_content" 
  12.         ohos:text="點我" 
  13.         ohos:text_size="200" 
  14.         ohos:background_element="red"> 
  15.      
  16.  
  17.  

 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)型:強(qiáng)轉(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)型:強(qiáng)轉(zhuǎn) 
  48.         Button but = (Button) component; 
  49.         but.setText("被點了"); 
  50.     } 

制運(yùn)行:

  • 點擊后:

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)型:強(qiáng)轉(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.     } 
  • 運(yùn)行:

  • 點擊后:

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. }); 

 運(yùn)行:

  • 當(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)做抽象方法的方法體。
  • 運(yùn)行:

6. 小節(jié)

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

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

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

https://harmonyos.


當(dāng)前標(biāo)題:HarmonyOS實戰(zhàn)—單擊事件的四種寫法
URL標(biāo)題:http://www.5511xx.com/article/codgoeh.html