HarmonyOS实战—实现双击事件 原创

兮动人
发布于 2021-7-28 10:35
1.1w浏览
1收藏

1. 双击事件

双击事件和单击事件有些类似,也有四种实现的方法

  • 实现步骤:
    1.通过id找到组件。
    2.给按钮组件设置双击事件。
    3.本类实现DoubleClickedListener接口重写。
    4.重写onDoubleClick方法

2. 实现案例

  • 当鼠标双击按钮后,Text文本内容就会发生变化
    HarmonyOS实战—实现双击事件-鸿蒙开发者社区
    HarmonyOS实战—实现双击事件-鸿蒙开发者社区

  • 新建项目 ListenerApplication2
    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">

    <Text
        ohos:id="$+id:text1"
        ohos:height="match_content"
        ohos:width="match_content"
        ohos:text="text"
        ohos:text_size="50">
    </Text>

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

</DirectionalLayout>
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.

MainAbilitySlice

package com.xdr630.listenerapplication2.slice;

import com.xdr630.listenerapplication2.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 implements Component.DoubleClickedListener {
    //把text1提为成员变量,不然onDoubleClick方法就访问不到
    //初始化默认值
    Text text1 = null;

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

        // 1.找到文本框组件和按钮组件
        text1 = (Text) findComponentById(ResourceTable.Id_text1);
        Button but1 = (Button) findComponentById(ResourceTable.Id_but1);

        // 2.绑定事件(想到点谁,就给谁绑定事件)
        // 当双击了but1按钮之后,就会执行本类中的 onDoubleClick 方法
        but1.setDoubleClickedListener(this);
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    @Override
    public void onDoubleClick(Component component) {
        //Component表示点击组件的对象
        //简单理解:我点了谁,那么 Component 就表示谁的对象
        //这里Component表示的是按钮对象

        //点击之后要做的是改变文本框中的内容
        text1.setText("双击");
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 运行:
    HarmonyOS实战—实现双击事件-鸿蒙开发者社区
  • 双击后:
    HarmonyOS实战—实现双击事件-鸿蒙开发者社区

©著作权归作者所有,如需转载,请注明出处,否则将追究法律责任
分类
4
收藏 1
回复
举报
4
1
回复
    相关推荐