​

一:事件的四种写法

<?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"/>

<Button
ohos:id="$+id:but1"
ohos:height="match_content"
ohos:width="match_content"
ohos:text="鐐规垜"
ohos:text_size="50"
ohos:background_element="red"
/>

</DirectionalLayout>
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;
import ohos.agp.components.Text;

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

Button but1 = (Button) findComponentById(ResourceTable.Id_but1);
text1 = (Text) findComponentById(ResourceTable.Id_text1);

//第一种
but1.setClickedListener(new MyListener());
//第二种
but1.setClickedListener(this);

//第三种
but1.setClickedListener(new Component.ClickedListener() {
@Override
public void onClick(Component component) {
Button btu = (Button) component;
btu.setText("被点了-单击事件的第三种写法");
text1.setText("被点击了");
}
});

//第四种
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("被点了-单击事件的第二种写法");
text1.setText("被点击了");
}
}


class MyListener implements Component.ClickedListener{

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

二:其他事件


  • 双击事件:DoubleClickedListener
  • 长按事件:LongClickedListener
  • 滑动事件:setTouchEventListener

//记录按下时手指的位置
float startX = 0;
float startY = 0;
@Override
public boolean onTouchEvent(Component component, TouchEvent touchEvent) {
count++;
//参数一:component表示滑动的那个组件。(布局也是一种组件)
//实际上此时代表的就是那个DirectionalLayout这个布局对象。
//参数二:touchEvent 动作对象(按下,滑动,抬起)
//获取当前手指对屏幕进行的操作(按下,滑动,抬起)
int action = touchEvent.getAction();
//1 表示按下操作
//2 表示松开操作
//3 表示滑动/移动操作
if(action == TouchEvent.PRIMARY_POINT_DOWN){
//只要写按下时需要运行的代码即可
//text1.setText("按下" + count);
//获取按下时手指的位置(坐标)
/* MmiPoint point = touchEvent.getPointerPosition(0);
startX = point.getX();
startY = point.getY();*/
//text1.setText(x + "---" + y);
text1.setText("按下");
}else if(action == TouchEvent.POINT_MOVE){
//移动/滑动
//text1.setText("移动"+ count);
/* MmiPoint point = touchEvent.getPointerPosition(0);
float x = point.getX();
float y = point.getY();
text1.setText(x + "---" + y);*/
text1.setText("移动");
}else if(action == TouchEvent.PRIMARY_POINT_UP){
text1.setText("松开");
//松开手指
//text1.setText("松开"+ count);
/* MmiPoint point = touchEvent.getPointerPosition(0);
float endX = point.getX();
float endY = point.getY();
//text1.setText(x + "---" + y);
//拿着按下时手指的位置跟松开时手指的位置进行比较就可以
if(endX > startX && Math.abs(endY - startY) < 100 ){
text1.setText("右滑");
}else if(endX < startX && Math.abs(endY - startY) < 100){
text1.setText("左滑");
}else if(endY > startY && Math.abs(endX - startX) < 100){
text1.setText("下滑");
}else if(endY < startY && Math.abs(endX - startX) < 100){
text1.setText("上滑");
}*/
}
//如果为true,表示所有的动作都会触发当前方法并执行对应代码。
//如果为false,表示只有第一个动作会触发当前方法并执行对应代码。
//后续的动作就不会触发当前方法了。
//按下 --- 移动 --- 松开
return true;
}