目录
一、简介
二、定义实现类
三、当前类作为实现类
四、匿名内部类
五、方法引用
六、HarmonyOS(鸿蒙)全面学习-精选好文汇总
一、简介
HarmonyOS(鸿蒙)开发过程中,使用到的最多的事件就是单击事件,单击事件一共有四种写法,它们有一些细微的区别和场景。 ?
四种写法如下:
- 定义实现类
- 当前类作为实现类
- 匿名内部类
- 方法引用
二、定义实现类
定义实现类ClickedListener实现Component.ClickedListener接口并且重写onClick方法
/**
?*?实现ClickedListener接口并重写onClick方法
?*/
class?ClickedListener?implements?Component.ClickedListener?{
????/**
?????*?点击事件触发的操作会调用的方法
?????*?@param?component?????被点击的组件对象
?????*/
????@Override
????public?void?onClick(Component?component)?{
????????//?具体点击操作的逻辑处理
????????Button?button?=?(Button)?component;
????????button.setText("哦,我被点击了!");
????}
}
在MainAbilitySlice中通过控件的setClickedListener方法传入Component.ClickedListener接口的实现类ClickedListener
public?class?MainAbilitySlice?extends?AbilitySlice?{
????@Override
????public?void?onStart(Intent?intent)?{
????????super.onStart(intent);
????????super.setUIContent(ResourceTable.Layout_ability_main);
????????//1.?找到组件
????????Button?button?=?(Button)?this.findComponentById(ResourceTable.Id_button);
????????//2.?绑定单击事件
????????button.setClickedListener(new?ClickedListener());
????}
????@Override
????public?void?onActive()?{
????????super.onActive();
????}
????@Override
????public?void?onForeground(Intent?intent)?{
????????super.onForeground(intent);
????}
}
三、当前类作为实现类
直接使用当前类MainAbilitySlice作为Component.ClickedListener接口的实现类,这个与上面的区别在于,我们不需要单独定义一个实现类,同时可以在onClick方法中共享MainAbilitySlice的定义的相关变量
public?class?MainAbilitySlice?extends?AbilitySlice?implements?Component.ClickedListener?{
????@Override
????public?void?onStart(Intent?intent)?{
????????super.onStart(intent);
????????super.setUIContent(ResourceTable.Layout_ability_main);
????????//1.?找到组件
????????Button?button?=?(Button)?this.findComponentById(ResourceTable.Id_button);
????????//2.?绑定单击事件
????????button.setClickedListener(this);
????}
????@Override
????public?void?onActive()?{
????????super.onActive();
????}
????@Override
????public?void?onForeground(Intent?intent)?{
????????super.onForeground(intent);
????}
????@Override
????public?void?onClick(Component?component)?{
????????//?具体点击操作的逻辑处理
????????Button?button?=?(Button)?component;
????????button.setText("哦,我被点击了!");
????}
}
四、匿名内部类
直接setClickedListener方法中传入匿名内部类,new Component.ClickedListener() {},这样做的坏处在于代码不可重复使用,并且如果这样的写法过多,会导致代码可读性降低
public?class?MainAbilitySlice?extends?AbilitySlice?{
????@Override
????public?void?onStart(Intent?intent)?{
????????super.onStart(intent);
????????super.setUIContent(ResourceTable.Layout_ability_main);
????????//1.?找到组件
????????Button?button?=?(Button)?this.findComponentById(ResourceTable.Id_button);
????????//2.?绑定单击事件
????????button.setClickedListener(new?Component.ClickedListener()?{
????????????@Override
????????????public?void?onClick(Component?component)?{
????????????????//?具体点击操作的逻辑处理
????????????????Button?button?=?(Button)?component;
????????????????button.setText("哦,我被点击了!");
????????????}
????????});
????????/*
?????????*?lambda写法
?????????button.setClickedListener(component?->?{
????????????//?具体点击操作的逻辑处理
????????????Button?button1?=?(Button)?component;
????????????button1.setText("哦,我被点击了!");
????????});
????????*/
????}
????@Override
????public?void?onActive()?{
????????super.onActive();
????}
????@Override
????public?void?onForeground(Intent?intent)?{
????????super.onForeground(intent);
????}
}
五、方法引用
这种写法,无需新增类,MainAbilitySlice类也无需实现Component.ClickedListener接口,而是通过手动写一个同名、同参数的onClick方法,通过方法引用的方式来实现 button.setClickedListener(this::onClick);
public?class?MainAbilitySlice?extends?AbilitySlice?{
????@Override
????public?void?onStart(Intent?intent)?{
????????super.onStart(intent);
????????super.setUIContent(ResourceTable.Layout_ability_main);
????????//1.?找到组件
????????Button?button?=?(Button)?this.findComponentById(ResourceTable.Id_button);
????????//2.?绑定单击事件
????????button.setClickedListener(this::onClick);
????}
????@Override
????public?void?onActive()?{
????????super.onActive();
????}
????@Override
????public?void?onForeground(Intent?intent)?{
????????super.onForeground(intent);
????}
????public?void?onClick(Component?component)?{
????????//?具体点击操作的逻辑处理
????????Button?button?=?(Button)?component;
????????button.setText("哦,我被点击了!");
????}
}
六、HarmonyOS(鸿蒙)全面学习-精选好文汇总
HarmonyOS(鸿蒙)DevEco Studio开发环境搭建
HarmonyOS(鸿蒙)开发一文入门
两个案例五分钟轻松入门Harmony(鸿蒙)开发
HarmonyOS与Android的全面对比
HarmonyOS(鸿蒙)全网最全资源汇总,吐血整理,赶紧收藏!
HarmonyOS(鸿蒙)—— Ability与页面
HarmonyOS(鸿蒙)——config.json详解
HarmonyOS(鸿蒙)——启动流程
HarmonyOS(鸿蒙)——全面入门,始于而不止于HelloWorld
?HarmonyOS(鸿蒙)——单击事件
|