05. 组件 —— 点击,滑动事件

  • 事件

  • 什么是事件

        事件:就是可以被文本、按钮、图片等组件识别的操作

  • 常见的事件   

        单击事件

        单击事件又叫点击事件。是开发使用最频繁的一种事件

        实现步骤:

        1. 通过id找到组件

        2. 给按钮组件设置单击事件

        3. 写一个ClickedListener接口,并重写onClick方法

        4. 编写onClick方法体(重点)

        例子:

        ability_main.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<DirectionalLayoutxmlns:ohos="http://schemas.huawei.com/res/ohos"ohos:height="match_parent"ohos:width="match_parent"ohos:alignment="center"ohos:orientation="vertical"><Buttonohos:id="$+id:but1"ohos:height="match_content"ohos:width="match_content"ohos:text="点我"ohos:text_size="200"ohos:background_element="red"/></DirectionalLayout>

        MainAbilitySlice.java

package com.example.myapplication.slice;import com.example.myapplication.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.Button;
import ohos.agp.components.Component;public class MainAbilitySlice extends AbilitySlice {@Overridepublic 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);//返回一个组件对象(是所有组件的父类对象)//那么可以在实际写代码中,向下转型Button but1 = (Button) findComponentById(ResourceTable.Id_but1);//2. 给按钮绑定一个单击事件(加上这个点击事件可以让其出现响应//                       ,没有这个系统将不进行任何响应)////通过一个点击事件来实现(setClickedListener里要添加自定义的MyListener类)//单击事件不只可以点一次,可以点多次but1.setClickedListener(new MyListener());}@Overridepublic void onActive() {super.onActive();}@Overridepublic void onForeground(Intent intent) {super.onForeground(intent);}
}class MyListener implements Component.ClickedListener {@Overridepublic void onClick(Component component) {//component:所有组件的父类//参数:被点击的组件对象Button btu = (Button) component;btu.setText("被点了");}
}

单击事件的四种写法 

        第一种 自定义实现类

        第一种刚才写过,这里就不做过多说明了。

        步骤:

        1. 通过id找到组件

        2. 给按钮组件设置单击事件

        3. 写一个ClickedListener接口,并重写onClick方法

        4. 编写onClick方法体(重点)        

        特点:想要在点击之后其他组件不需要被调用,则用这个方法。

        MainAbilitySlice.java

package com.example.myapplication.slice;import com.example.myapplication.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.Button;
import ohos.agp.components.Component;public class MainAbilitySlice extends AbilitySlice {@Overridepublic 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());}@Overridepublic void onActive() {super.onActive();}@Overridepublic void onForeground(Intent intent) {super.onForeground(intent);}
}class MyListener implements Component.ClickedListener {@Overridepublic void onClick(Component component) {Button btu = (Button) component;btu.setText("被点了");}
}

       

         第二种 当前类作为实现类

        特点:想要在点击之后其他组件需要被调用,则用这个方法。

        ability_main.xml