HarmonyOS實戰(zhàn)—單擊事件的四種寫法
作者:兮動人
編寫實現(xiàn)類(MyListener)去實現(xiàn) Component.ClickedListener 接口,在類里面重新下 onClick 方法,把點擊代碼實現(xiàn)的操作就寫在 onClick 方法當(dāng)中。
51CTO和華為官方合作共建的鴻蒙技術(shù)社區(qū)
單擊事件的四種寫法

1. 自己編寫實現(xiàn)類
- 編寫實現(xiàn)類(MyListener)去實現(xiàn) Component.ClickedListener 接口
- 在類里面重新下 onClick 方法,把點擊代碼實現(xiàn)的操作就寫在 onClick 方法當(dāng)中
- 實現(xiàn)代碼:
- 創(chuàng)建項目名為:ListenerApplication
ability_main.xml
- <?xml version="1.0" encoding="utf-8"?>
- <DirectionalLayout
- xmlns:ohos="http://schemas.huawei.com/res/ohos"
- ohos:height="match_parent"
- ohos:width="match_parent"
- ohos:alignment="center"
- ohos:orientation="vertical">
- <Button
- ohos:id="$+id:but1"
- ohos:height="match_content"
- ohos:width="match_content"
- ohos:text="點我"
- ohos:text_size="200"
- ohos:background_element="red">
- </Button>
- </DirectionalLayout>
MainAbilitySlice
- package com.example.listenerapplication.slice;
- import com.example.listenerapplication.ResourceTable;
- import ohos.aafwk.ability.AbilitySlice;
- import ohos.aafwk.content.Intent;
- import ohos.agp.components.Button;
- import ohos.agp.components.Component;
- public class MainAbilitySlice extends AbilitySlice {
- @Override
- public void onStart(Intent intent) {
- super.onStart(intent);
- super.setUIContent(ResourceTable.Layout_ability_main);
- //1.找到按鈕
- //完整寫法:this.findComponentById(ResourceTable.Id_but1);
- //this:本類的對象,指:MainAbilitySlice(子界面對象)
- // 在子界面當(dāng)中,通過 id 找到對應(yīng)的組件
- // 用this去調(diào)用方法,this可以省略不寫
- //findComponentById(ResourceTable.Id_but1);
- //返回一個組件對象(所以組件的父類對象)
- //那么我們在實際寫代碼的時候,需要向下轉(zhuǎn)型:強轉(zhuǎn)
- Component but1 = (Button) findComponentById(ResourceTable.Id_but1);
- //2.給按鈕綁定單擊事件,當(dāng)點擊后,就會執(zhí)行 MyListener 中的方法,點一次執(zhí)行一次
- // 而方法就是下面點擊的內(nèi)容
- but1.setClickedListener(new MyListener());
- }
- @Override
- public void onActive() {
- super.onActive();
- }
- @Override
- public void onForeground(Intent intent) {
- super.onForeground(intent);
- }
- }
- class MyListener implements Component.ClickedListener{
- @Override
- public void onClick(Component component) {
- //Component:所有組件的父類
- //component參數(shù): 被點擊的組件對象,在這里就表示按你的對象
- //component.setText(); setText是子類特有的方法,需要向下轉(zhuǎn)型:強轉(zhuǎn)
- Button but = (Button) component;
- but.setText("被點了");
- }
- }
制運行:
- 點擊后:
2. 當(dāng)前類實現(xiàn)接口
- ability_main.xml 中把ohos:text_size="50",其他跟上面一樣不變
- MainAbilitySlice 中只需把上面新建的類 MyListener 給去掉,然后 AbilitySlice 實現(xiàn) ClickedListener 接口類中的 onClick 方法,給本類的 but1按鈕直接綁定單價事件
- package com.example.listenerapplication.slice;
- import com.example.listenerapplication.ResourceTable;
- import ohos.aafwk.ability.AbilitySlice;
- import ohos.aafwk.content.Intent;
- import ohos.agp.components.Button;
- import ohos.agp.components.Component;
- public class MainAbilitySlice extends AbilitySlice implements Component.ClickedListener {
- @Override
- public void onStart(Intent intent) {
- super.onStart(intent);
- super.setUIContent(ResourceTable.Layout_ability_main);
- //1.找到按鈕
- //完整寫法:this.findComponentById(ResourceTable.Id_but1);
- //this:本類的對象,指:MainAbilitySlice(子界面對象)
- // 在子界面當(dāng)中,通過 id 找到對應(yīng)的組件
- // 用this去調(diào)用方法,this可以省略不寫
- //findComponentById(ResourceTable.Id_but1);
- //返回一個組件對象(所以組件的父類對象)
- //那么我們在實際寫代碼的時候,需要向下轉(zhuǎn)型:強轉(zhuǎn)
- Component but1 = (Button) findComponentById(ResourceTable.Id_but1);
- //2.給but1綁定單擊事件,當(dāng)事件被觸發(fā)后,就會執(zhí)行本類中的onClick方法,this就代表本類
- but1.setClickedListener(this);
- }
- @Override
- public void onActive() {
- super.onActive();
- }
- @Override
- public void onForeground(Intent intent) {
- super.onForeground(intent);
- }
- @Override
- public void onClick(Component component) {
- Button but = (Button) component;
- but.setText("被點了——單擊事件的第二種寫法");
- }
- }
- 運行:
- 點擊后:
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)容是一樣的),如下:
- but1.setClickedListener(new Component.ClickedListener() {
- @Override
- public void onClick(Component component) {
- Button but = (Button) component;
- but.setText("被點了——單擊事件的第三種寫法");
- text1.setText("被點擊了");
- }
- });
運行:
- 當(dāng)被點擊后,觸發(fā)了 onClick 方法中兩個設(shè)置文本的方法(Button和Text文本都發(fā)生了變化)
5. 方法引用
- 這個方法的形參和方法的返回值類型需要跟接口里的抽象方法里的形參和返回值類型要保持一致
- 代碼實現(xiàn),布局代碼不變跟匿名內(nèi)部類的一致,改動如下:
- 直接編寫 onClick 方法,不帶 @Override ,然后在 onStart 方法中直接調(diào)用即可
- package com.example.listenerapplication.slice;
- import com.example.listenerapplication.ResourceTable;
- import ohos.aafwk.ability.AbilitySlice;
- import ohos.aafwk.content.Intent;
- import ohos.agp.components.Button;
- import ohos.agp.components.Component;
- import ohos.agp.components.Text;
- public class MainAbilitySlice extends AbilitySlice {
- Text text1 = null;
- @Override
- public void onStart(Intent intent) {
- super.onStart(intent);
- super.setUIContent(ResourceTable.Layout_ability_main);
- Component but1 = (Button) findComponentById(ResourceTable.Id_but1);
- text1 = (Text) findComponentById(ResourceTable.Id_text1);
- but1.setClickedListener(this::onClick);
- }
- @Override
- public void onActive() {
- super.onActive();
- }
- @Override
- public void onForeground(Intent intent) {
- super.onForeground(intent);
- }
- public void onClick(Component component) {
- Button but = (Button) component;
- but.setText("被點了——單擊事件的第四種寫法");
- text1.setText("被點擊了");
- }
- }
- 當(dāng)按鈕被點擊后,就要執(zhí)行this本類中的onClick方法,相當(dāng)于把下面的public void onClick...方法拿過來,引用了一下,當(dāng)做抽象方法的方法體。
- 運行:
6. 小節(jié)
當(dāng)前類作為實現(xiàn)類和方法引用是比較常用的。其他的寫法也要掌握了解即可。
51CTO和華為官方合作共建的鴻蒙技術(shù)社區(qū)
責(zé)任編輯:jianghua
來源:
鴻蒙社區(qū)