2024年鸿蒙HarmonyOS APP 开发入门2--事件_鸿蒙os 按钮事件(1),【大牛系列教学

深知大多数程序员,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!


img
img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上鸿蒙开发知识点,真正体系化!

由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新

需要这份系统化的资料的朋友,可以戳这里获取

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("被点了");
}

}


​ 2)当前类实现接口



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

    //1.找到按钮
    
    Button but1 = (Button) findComponentById(ResourceTable.Id_but1);
    
    text1 = (Text) findComponentById(ResourceTable.Id_text1);
    
    //2.给按钮绑定一个单击事件
    
    but1.setClickedListener(this);
}
@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("被点击了");
}

}


​ 3)匿名内部类



public class MainAbilitySlice extends AbilitySlice {
Text text1 = null;
@Override
public void onStart(Intent intent) {

    super.onStart(intent);
    
    super.setUIContent(ResourceTable.Layout_ability_main);
    
    //1.找到按钮
    
    Button but1 = (Button) findComponentById(ResourceTable.Id_but1);
    
    text1 = (Text) findComponentById(ResourceTable.Id_text1);
    
    //2.给按钮绑定一个单击事件
    
    but1.setClickedListener(new Component.ClickedListener() {
    @Override
        public void onClick(Component component) {
            
            Button btu = (Button) component;
            
            btu.setText("被点了-单击事件的第三种写法");
            
            text1.setText("被点击了");
        }
    });
}
@Override
public void onActive() {
    super.onActive();
    }
@Override
public void onForeground(Intent intent) {
    super.onForeground(intent);
    }

}


​ 4)方法引用



public class MainAbilitySlice extends AbilitySlice {
Text text1 = null;
@Override
public void onStart(Intent intent) {

    super.onStart(intent);
    
    super.setUIContent(ResourceTable.Layout_ability_main);
    
    
    //1.找到按钮
    
    Button but1 = (Button) findComponentById(ResourceTable.Id_but1);
    
    text1 = (Text) findComponentById(ResourceTable.Id_text1);
    
    //2.给按钮绑定一个单击事件
    
    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("被点击了");
}

}


##### 3.双击事件




---


接口名:DoubleClickedListener


案例:双击按钮修改文本内



public class MainAbilitySlice extends AbilitySlice implements Component.DoubleClickedListener {

Text cyy_text;

@Override
public void onStart(Intent intent) {
    super.onStart(intent);
    super.setUIContent(ResourceTable.Layout_ability_main);

    //通过id找到组件



    cyy_text = (Text) findComponentById(ResourceTable.Id_cyy_text);
    Button cyy_button = findComponentById(ResourceTable.Id_cyy_button);


    //绑定事件

    cyy_button.setDoubleClickedListener(this);
}

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

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

@Override
public void onDoubleClick(Component component) {

    //双击事件

    cyy_text.setText("lms");
    cyy_text.setTextColor(Color.RED);

}

}


##### 4.长按事件


​ 接口名:LongClickedListener


​ 案例:长按按钮修改文本内容



public class MainAbilitySlice extends AbilitySlice implements Component.LongClickedListener {
Text cyy_text;
@Override
public void onStart(Intent intent) {
super.onStart(intent);
super.setUIContent(ResourceTable.Layout_ability_main);

    //通过id找到组件


    cyy_text = (Text) findComponentById(ResourceTable.Id_cyy_text);

    Button cyy_button = (Button) findComponentById(ResourceTable.Id_cyy_button);

    //绑定事件

    cyy_button.setLongClickedListener(this);
}

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

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

@Override
public void onLongClicked(Component component) {

    //长按事件
    
    cyy_text.setText("lms");
    cyy_text.setTextColor(Color.RED);

}

}


##### 5. 滑动事件


​


接口名:TouchEventListener


包括三个动作对象:按下,移动,抬起


* PRIMARY\_POINT\_DOWN:按下
* POINT\_MOVE:移动
* PRIMARY\_POINT\_UP:抬起


手机坐标


* 手机左上角的点为原点
* 向右为X轴
* 向下为Y轴
* 垂直于屏幕向上为Z轴


方法返回值


* ​ true表示继续执行后面的动作
* false表示不会继续执行后面的动作



public class MainAbilitySlice extends AbilitySlice implements Component.TouchEventListener {

Text cyy_Text = null;
int count = 0;


@Override
public void onStart(Intent intent) {
    super.onStart(intent);
    super.setUIContent(ResourceTable.Layout_ability_main);

    //先找到整个的布局对象

    DirectionalLayout cyy_directional_layout = (DirectionalLayout) findComponentById(ResourceTable.Id_cyy_dl);

深知大多数程序员,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!


img
img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上鸿蒙开发知识点,真正体系化!

由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新

需要这份系统化的资料的朋友,可以戳这里获取

.csdnimg.cn/direct/743b668910224b259a5ffe804fa6d0db.png)
[外链图片转存中…(img-p50m21ki-1715720854497)]
[外链图片转存中…(img-80hgQbUm-1715720854497)]

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上鸿蒙开发知识点,真正体系化!

由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新

需要这份系统化的资料的朋友,可以戳这里获取

  • 7
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值