天天看點

HarmonyOS實戰—單擊事件的四種寫法單擊事件的四種寫法

單擊事件的四種寫法

HarmonyOS實戰—單擊事件的四種寫法單擊事件的四種寫法

1. 自己編寫實作類

  • 編寫實作類(MyListener)去實作 Component.ClickedListener 接口
  • 在類裡面重新下 onClick 方法,把點選代碼實作的操作就寫在 onClick 方法當中
  • 實作代碼:
HarmonyOS實戰—單擊事件的四種寫法單擊事件的四種寫法
  • 建立項目名為:ListenerApplication
HarmonyOS實戰—單擊事件的四種寫法單擊事件的四種寫法

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(子界面對象)
        // 在子界面當中,通過 id 找到對應的元件
        // 用this去調用方法,this可以省略不寫
        //findComponentById(ResourceTable.Id_but1);
        //傳回一個元件對象(是以元件的父類對象)
        //那麼我們在實際寫代碼的時候,需要向下轉型:強轉
        Component but1 = (Button) findComponentById(ResourceTable.Id_but1);

        //2.給按鈕綁定單擊事件,當點選後,就會執行 MyListener 中的方法,點一次執行一次
        // 而方法就是下面點選的内容
        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參數: 被點選的元件對象,在這裡就表示按你的對象
        //component.setText(); setText是子類特有的方法,需要向下轉型:強轉
        Button but = (Button) component;
        but.setText("被點了");
    }
}           
  • 運作:
HarmonyOS實戰—單擊事件的四種寫法單擊事件的四種寫法
  • 點選後:
HarmonyOS實戰—單擊事件的四種寫法單擊事件的四種寫法

2. 目前類實作接口

  • ability_main.xml 中把

    ohos:text_size="50"

    ,其他跟上面一樣不變
  • MainAbilitySlice 中隻需把上面建立的類 MyListener 給去掉,然後 AbilitySlice 實作 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(子界面對象)
        // 在子界面當中,通過 id 找到對應的元件
        // 用this去調用方法,this可以省略不寫
        //findComponentById(ResourceTable.Id_but1);
        //傳回一個元件對象(是以元件的父類對象)
        //那麼我們在實際寫代碼的時候,需要向下轉型:強轉
        Component but1 = (Button) findComponentById(ResourceTable.Id_but1);

        //2.給but1綁定單擊事件,當事件被觸發後,就會執行本類中的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("被點了——單擊事件的第二種寫法");
    }
}           
HarmonyOS實戰—單擊事件的四種寫法單擊事件的四種寫法
HarmonyOS實戰—單擊事件的四種寫法單擊事件的四種寫法

3. 自己編寫實作類 和 目前類實作接口 的差別

  • 如果添加在按鈕上面添加一個Text文本内容,當按鈕點選後就會修改文本框的内容
  • 改動第一個案例中的代碼:添加Text文本框
HarmonyOS實戰—單擊事件的四種寫法單擊事件的四種寫法
HarmonyOS實戰—單擊事件的四種寫法單擊事件的四種寫法
  • 上面的

    onStart

    方法中

    text1

    是局部變量,如果用第一種方法(自己編寫實作類)來寫,

    MyListener

    不能調用到

    text1

    變量
HarmonyOS實戰—單擊事件的四種寫法單擊事件的四種寫法
  • 如果使用第二種方法(目前類實作接口),就要把

    text1

    提到成員變量,再把設定點選後的内容添加到

    onClick

HarmonyOS實戰—單擊事件的四種寫法單擊事件的四種寫法
  • 如果在點選按鈕之後,需要操作其他的元件對象,那麼就可以使用第二種方式(目前類實作接口)。
  • 如果在點選按鈕之後,不需要操作其他的元件對象,就可以使用第一種方式(自己編寫實作類)。

4. 匿名内部類

  • 采用匿名内部類就不需要實作

    implement ClickedListener

    接口,也不需要再建立一個類了
  • 但使用匿名内部類的代碼隻能使用一次。當使用代碼需要用一次的時候,可以采用匿名内部類的形式來簡化代碼
  • 直接

    new ClickedListener

    就能實作了,然後把第一種實作方式(自己編寫實作類)中的

    onClick

    拿過來或第二種方式(目前類實作接口)實作的

    onClick

    方法拿過來就行了(其實這兩者的onClick方法的内容是一樣的),如下:
but1.setClickedListener(new Component.ClickedListener() {
    @Override
    public void onClick(Component component) {
        Button but = (Button) component;
        but.setText("被點了——單擊事件的第三種寫法");
        text1.setText("被點選了");
    }
});           
HarmonyOS實戰—單擊事件的四種寫法單擊事件的四種寫法
  • 當被點選後,觸發了

    onClick

    方法中兩個設定文本的方法(

    Button

    Text

    文本都發生了變化)
HarmonyOS實戰—單擊事件的四種寫法單擊事件的四種寫法

5. 方法引用

  • 這個方法的形參和方法的傳回值類型需要跟接口裡的抽象方法裡的形參和傳回值類型要保持一緻
  • 代碼實作,布局代碼不變跟匿名内部類的一緻,改動如下:
  • 直接編寫

    onClick

    方法,不帶

    @Override

    ,然後在

    onStart

    方法中直接調用即可
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("被點選了");
    }
}           
HarmonyOS實戰—單擊事件的四種寫法單擊事件的四種寫法
  • 當按鈕被點選後,就要執行this本類中的

    onClick

    方法,相當于把下面的

    public void onClick...

    方法拿過來,引用了一下,當做抽象方法的方法體。
HarmonyOS實戰—單擊事件的四種寫法單擊事件的四種寫法
HarmonyOS實戰—單擊事件的四種寫法單擊事件的四種寫法

6. 小節

  • 目前類作為實作類和方法引用是比較常用的。其他的寫法也要掌握了解即可。

繼續閱讀