zoukankan      html  css  js  c++  java
  • HarmonyOS实战—实现单击事件流程

    1. 什么是事件?

    • 事件就是可以被识别的操作 。就是可以被文本、按钮、图片等组件识别的操作。
    • 常见的事件有:单击、双击、长按、还有触摸事件
    • 可以给文本、按钮等添加不同的事件。比如添加了单击事件之后,当我们再次点击文本、按钮,就可以运行对应的代码了。
    • 常见的事件有:
      在这里插入图片描述

    2. 单击事件(常用)

    • 单击事件:又叫做点击事件。是开发中使用最多的一种事件,没有之一。
    • 接口名:ClickedListener,又叫:点击事件。
    • 如:当点击后,文字内容就会发送变化
      在这里插入图片描述
      在这里插入图片描述

    3. 实现步骤

    • 创建项目名为: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(子界面对象)
            // 在子界面当中,通过 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("被点了");
        }
    }
    
    • 运行:
      在这里插入图片描述
    • 点击后:
      在这里插入图片描述

    4. 单击事件小节

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

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

    • 【本文正在参与“有奖征文 | HarmonyOS征文大赛”活动】
      https://marketing.csdn.net/p/ad3879b53f4b8b31db27382b5fc65bbc

    本文来自博客园,作者:兮动人,转载请注明原文链接:https://www.cnblogs.com/xdr630/p/15254575.html

  • 相关阅读:
    Mongodb介绍(非原创)
    软件架构设计常用方法-软件架构设计学习第五天(非原创) 发布成功,点击查看文章
    浅谈Javascript中的原型、原型链、继承
    GitHub当作私密的版本控制系统远端版本库私有化
    TortoiseGit 绑定 GitHub/Gitee ssh秘钥
    一次停电引发服务器硬件故障
    sed往指定位置插入变量的小技巧
    SQL server 2008 R2 安装步骤
    Sublime Text提示Unable to download XXX. Please view the console for more details安装插件失败解决
    Sublime Text3配置Lua运行环境
  • 原文地址:https://www.cnblogs.com/xdr630/p/15254575.html
Copyright © 2011-2022 走看看