HarmonyOS实战—实现长按事件 原创 精华

兮动人
发布于 2021-7-28 11:24
浏览
3收藏

1. 长按事件

  • 长按事件使用的次数不是很多,但在有些特殊的情况下还是要用到的。
  • 比如:复制一段文字的时候就是长按操作
  • 长按事件和单、双击事件也非常类似
  • 接口名:LongClickedListener

2. 实现案例:长按按钮修改文本内容

  • 新建项目:ListenerApplication3

ability_main

<?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="100">

    </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.
  • 27.

MainAbilitySlice

package com.xdr630.listenerapplication3.slice;

import com.xdr630.listenerapplication3.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.LongClickedListener {
    //提为成员变量,否则onLongClicked访问不到文本组件,并初始化默认值
    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.绑定长按事件,点谁就给谁绑定事件
        //当对按钮进行长按操作时,就会执行this本类中onLongClicked方法
        but1.setLongClickedListener(this);
    }

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

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

    @Override
    public void onLongClicked(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.
  • 运行:

HarmonyOS实战—实现长按事件-鸿蒙开发者社区

  • 长按按钮后:
    HarmonyOS实战—实现长按事件-鸿蒙开发者社区

©著作权归作者所有,如需转载,请注明出处,否则将追究法律责任
分类
已于2021-7-29 11:38:59修改
7
收藏 3
回复
举报
7
3
3
3条回复
按时间正序
/
按时间倒序
Anzia
Anzia

wow,我还是第一次知道原来有长按的方法

回复
2021-7-28 21:32:25
兮动人
兮动人 回复了 Anzia
wow,我还是第一次知道原来有长按的方法

安卓有的鸿蒙基本都有,哈哈。

回复
2021-7-28 22:43:12
已注销
已注销

安卓的没有,鸿蒙的还有。

回复
2021-11-8 11:02:57


回复
    相关推荐