HarmonyOS开发01:单击事件

单击事件:又叫做点击事件,是开发中使用最多的一种事件,没有之一
接口名:ClickedListener

实现步骤:

  1. 通过id找到组件
  2. 给按钮组件设置单击事件
  3. 写一个类实现ClickedListener接口并重写onClick方法
  4. 编写onClick方法体

首先修改ability_main.xml文件
添加:

<Button
        ohos:id="$+id:but1"
        ohos:height="match_content"
        ohos:width="match_content"
        ohos:text="点我"
        ohos:text_size="200"
        ohos:background_element="red"
        />

方法一:定义实现类

定义MyListener类:

class MyListener implements Component.ClickedListener {
    
    @Override
    public void onClick(Component component) {
        //component : 所有组件的父类
        //参数 :  被点击的组件对象
        Button btu = (Button) component;
        btu.setText("被点了");
    }
}

整体代码:

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.找到按钮
        Button but1 = (Button) findComponentById(ResourceTable.Id_but1);

        //2.给按钮绑定单击事件
        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 : 所有组件的父类
        //参数 :  被点击的组件对象
        Button btu = (Button) component;
        btu.setText("被点了");
    }
}

onClick不方便调用其它组件

方法二 : 当前类作为实现类(常用)

当前类继承 Component.ClickedListener

public class MainAbilitySlice extends AbilitySlice implements Component.ClickedListener{
public void onClick(Component component) {

        Button btu = (Button) component;
        btu.setText("被点了-1");
    }
}

整体代码:

public class MainAbilitySlice extends AbilitySlice implements Component.ClickedListener {
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);

        //1.找到按钮
        Button but1 = (Button) findComponentById(ResourceTable.Id_but1);

        //2.给按钮绑定单击事件
        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 btu = (Button) component;
        btu.setText("被点了-2");
    }
}

onClick方便调用其它组件

方法三 : 匿名内部类

整体代码:

public class MainAbilitySlice extends AbilitySlice {
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);

        //1.找到按钮
        Button but1 = (Button) findComponentById(ResourceTable.Id_but1);

        //2.给按钮绑定单击事件
        but1.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                Button btu = (Button) component;
                btu.setText("被点了-3");
            }
        });
    }

缺点:只能用一次

方法四 : 方法引用(常用)

public class MainAbilitySlice extends AbilitySlice {
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);

        //1.找到按钮
        Button but1 = (Button) findComponentById(ResourceTable.Id_but1);

        //2.给按钮绑定单击事件
        //but1.setClickedListener(new MyListener());

        //but1.setClickedListener(this);

        /*but1.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                Button btu = (Button) component;
                btu.setText("被点了-3");
            }
        });*/

        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 btu = (Button) component;
        btu.setText("被点了-4");
    }
}

你可能感兴趣的:(鸿蒙开发,华为,软件开发,java)