回复
Harmony OS 滑动事件开发-JAVA
HUAWEI_Engineer
发布于 2021-10-28 17:12
浏览
0收藏
1.在ability_main.xml
<?xml version="1.0" encoding="utf-8"?>
<DirectionalLayout
ohos:id="$+id:dl"
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:tx"
ohos:height="match_content"
ohos:width="match_content"
ohos:text="滑动事件"
ohos:text_size="25vp"
ohos:text_color="green"
/>
</DirectionalLayout>
2.MainAbilitySlice
public class MainAbilitySlice extends AbilitySlice implements Component.TouchEventListener {
Text text;
int count = 0;//定义一个成员变量,验证touchEvent方法的调用
@Override
public void onStart(Intent intent) {
super.onStart(intent);
super.setUIContent(ResourceTable.Layout_ability_main);
//1.获取布局对象和文本
DirectionalLayout dl =(DirectionalLayout)findComponentById(ResourceTable.Id_dl);
text = (Text)findComponentById(ResourceTable.Id_tx);
//2.给整个布局添加滑动事件
dl.setTouchEventListener(this);
}
@Override
public void onActive() {
super.onActive();
}
@Override
public void onForeground(Intent intent) {
super.onForeground(intent);
}
@Override
public boolean onTouchEvent(Component component, TouchEvent touchEvent) {
//component 是表示滑动的那个组件
//实际代表的就是 DirectionalLayout的对象
//touchEvent代表(上划,下滑,抬起)
count++;//count成员变量每次加以,加以验证touchEvent方法的调用
int action = touchEvent.getAction();
//1.CTRL+B 查看touchEvent里封装的方法
//public static final int POINT_MOVE = 3;//3是移动
//public static final int PRIMARY_POINT_DOWN = 1;//1是按下
//public static final int PRIMARY_POINT_UP = 2;//2是松开
if (action==TouchEvent.PRIMARY_POINT_DOWN){
text.setText("按下" +count);
// MmiPoint pointerPosition = touchEvent.getPointerPosition(0);
// float x= pointerPosition.getX();
// float y =pointerPosition.getY();
// text.setText(x +"----"+y);
}else if (action==TouchEvent.POINT_MOVE){
text.setText("移动" +count);
// MmiPoint pointerPosition = touchEvent.getPointerPosition(0);
// float x= pointerPosition.getX();
// float y =pointerPosition.getY();
// text.setText(x +"----"+y);
}else if (action==TouchEvent.PRIMARY_POINT_UP){
text.setText("抬起" +count);
// MmiPoint pointerPosition = touchEvent.getPointerPosition(0);
// float x= pointerPosition.getX();
// float y =pointerPosition.getY();
// text.setText(x +"----"+y);
}
return true;
}
}
效果图:
标签
Harmony.rar 1.19M 10次下载
赞
1
收藏
回复
相关推荐