HarmonyOS鸿蒙)——单击事件的四种写法

网友投稿 1236 2022-05-30

一、简介

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("哦,我被点击了!"); } }

Java 分布式 移动APP

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:连续两年入选Gartner®云数据库管理系统魔力象限™
下一篇:fcntl函数
相关文章