当前位置:   article > 正文

[HarmonyOS]——组件&布局&事件概念明确及点击事件_harmony os屏幕点击事件

harmony os屏幕点击事件

概念明确

组件:屏幕展示出来的元素,都称之为组件

布局:多个组件的摆放方式就是布局,组件必须添加到布局中才能显示出来

事件:就是可以被组件识别的操作

 一、事件介绍

1、什么是事件

事件就是可以被组件识别的操作,也即:就是可以被文本、按钮、图片等组件识别的操作。

2、常见事件

 二、单击事件

单击事件又称点击事件,是开发中使用最多的一种事件,没有之一。

1、单击事件实现步骤

  1. 通过id找到组件
  2. 给按钮组件设置单击事件
  3. 写一个类实现 ClickedListener 接口并重写onClick方法
  4. 编写onClick方法体

2、ResourceTable解读

在查找对应的组件时,是使用ResourceTable类+对应组件id的方式找到对应对象。ResourceTable是项目的资源列表类,不需要人为的去定义,只要在Resource中Base下的layout中定义了对应的资源,在运行期间,build中就会生成对应的资源列表,详情如下:

3、单击事件的4种实现方法

下图列出了4种实现方式,但是在实际开发中,常用的有:(1)当前类作为实现类(2)方法引用

XML文件中定义了Button组件:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <DirectionalLayout
  3. xmlns:ohos="http://schemas.huawei.com/res/ohos"
  4. ohos:height="match_parent"
  5. ohos:width="match_parent"
  6. ohos:alignment="center"
  7. ohos:orientation="vertical">
  8. <Button
  9. ohos:id="$+id:but1"
  10. ohos:height="match_content"
  11. ohos:width="match_content"
  12. ohos:background_element="red"
  13. ohos:text_size="100"
  14. ohos:text="点击事件"
  15. />
  16. </DirectionalLayout>

(1)法1:定义实现类方式

在实现类中,onClick方法中的参数component代表要操作的组件,为了能使用对应组建的方法,需要使用强制类型转换

  1. package com.example.clickthings.slice;
  2. import com.example.clickthings.ResourceTable;
  3. import ohos.aafwk.ability.AbilitySlice;
  4. import ohos.aafwk.content.Intent;
  5. import ohos.agp.components.Button;
  6. import ohos.agp.components.Component;
  7. public class MainAbilitySlice extends AbilitySlice {
  8. @Override
  9. public void onStart(Intent intent) {
  10. super.onStart(intent);
  11. super.setUIContent(ResourceTable.Layout_ability_main);
  12. //1、查找组件
  13. Button button = (Button)findComponentById(ResourceTable.Id_but1);
  14. //2、给按钮设置单击事件
  15. button.setClickedListener(new MyListener());
  16. }
  17. @Override
  18. public void onActive() {
  19. super.onActive();
  20. }
  21. @Override
  22. public void onForeground(Intent intent) {
  23. super.onForeground(intent);
  24. }
  25. }
  26. class MyListener implements Component.ClickedListener {
  27. @Override
  28. public void onClick(Component component) {
  29. ((Button)component).setText("已经点击了");
  30. }
  31. }

(2)法2:当前类作为实现类

  1. public class MainAbilitySlice extends AbilitySlice implements Component.ClickedListener {
  2. @Override
  3. public void onStart(Intent intent) {
  4. super.onStart(intent);
  5. super.setUIContent(ResourceTable.Layout_ability_main);
  6. //1、查找组件
  7. Button button = (Button)findComponentById(ResourceTable.Id_but1);
  8. //2、给按钮设置单击事件
  9. button.setClickedListener(this);
  10. }
  11. @Override
  12. public void onActive() {
  13. super.onActive();
  14. }
  15. @Override
  16. public void onForeground(Intent intent) {
  17. super.onForeground(intent);
  18. }
  19. @Override
  20. public void onClick(Component component) {
  21. ((Button)component).setText("已经点击了");
  22. }
  23. }

(3)法3:匿名内部类

  1. public class MainAbilitySlice extends AbilitySlice{
  2. @Override
  3. public void onStart(Intent intent) {
  4. super.onStart(intent);
  5. super.setUIContent(ResourceTable.Layout_ability_main);
  6. //1、查找组件
  7. Button button = (Button)findComponentById(ResourceTable.Id_but1);
  8. //2、给按钮设置单击事件
  9. button.setClickedListener(new Component.ClickedListener() {
  10. @Override
  11. public void onClick(Component component) {
  12. ((Button)component).setText("已经点击了");
  13. }
  14. });
  15. }
  16. @Override
  17. public void onActive() {
  18. super.onActive();
  19. }
  20. @Override
  21. public void onForeground(Intent intent) {
  22. super.onForeground(intent);
  23. }
  24. }

(4)法4:方法引用

直接在对应的XXXAbilitySlice类定义onClick()方法写点击需要进行的操作。

  1. public class MainAbilitySlice extends AbilitySlice{
  2. @Override
  3. public void onStart(Intent intent) {
  4. super.onStart(intent);
  5. super.setUIContent(ResourceTable.Layout_ability_main);
  6. //1、查找组件
  7. Button button = (Button)findComponentById(ResourceTable.Id_but1);
  8. //2、给按钮设置单击事件
  9. button.setClickedListener(this::onClick);
  10. }
  11. @Override
  12. public void onActive() {
  13. super.onActive();
  14. }
  15. @Override
  16. public void onForeground(Intent intent) {
  17. super.onForeground(intent);
  18. }
  19. public void onClick(Component component) {
  20. ((Button)component).setText("已经点击了");
  21. }
  22. }

笔记本人辛苦整理,转载复制望加文章出处,并推荐【Star星屹程序设计】,谢谢配合

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/258605
推荐阅读
相关标签
  

闽ICP备14008679号