当前位置:   article > 正文

DevEco Studio 的组件布局和事件内容(笔记)_deveco studio 控件和事件

deveco studio 控件和事件

本文选择的是Java开发模式,不是Js+Java的开发模式

一、组件、布局、事件

组件:屏幕展示出来的元素,文本、按钮、图片等。

布局:组件摆放方式

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

二、事件

常见的有:单击、双击、长按、滑动等

1.单击事件

第一种写法

自己定义实现类

修改ability_main.xml

  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:text="click me"
  13. ohos:text_size="100"
  14. ohos:background_element="red"
  15. />
  16. </DirectionalLayout>

为对应的页面写上如下代码

  1. package com.example.myapplication.slice;
  2. import com.example.myapplication.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. Button but1 = (Button)findComponentById(ResourceTable.Id_but1);
  13. but1.setClickedListener((new MyListener()));
  14. }
  15. @Override
  16. public void onActive() {
  17. super.onActive();
  18. }
  19. @Override
  20. public void onForeground(Intent intent) {
  21. super.onForeground(intent);
  22. }
  23. }
  24. class MyListener implements Component.ClickedListener{
  25. @Override
  26. //传入的参数指的是被点击的控件
  27. public void onClick(Component component){
  28. Button but = (Button)component;
  29. but.setText("Clicked");
  30. }
  31. }

第二种写法

当前类作为实现类,即上一篇文章中的用法

不过这里将增添一个文本,让其随点击事件而变化

由此可以知,第二种写法可以较方便的控制页面中其他组件。当然,第一种可以改写构造函数来实现。

  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. <Text
  9. ohos:id="$+id:text1"
  10. ohos:height="match_content"
  11. ohos:width="match_content"
  12. ohos:text="Wait"
  13. ohos:text_size="100"
  14. />
  15. <Button
  16. ohos:id="$+id:but1"
  17. ohos:height="match_content"
  18. ohos:width="match_content"
  19. ohos:text="click me"
  20. ohos:text_size="100"
  21. ohos:background_element="red"
  22. />
  23. </DirectionalLayout>
  1. package com.example.myapplication.slice;
  2. import com.example.myapplication.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. import ohos.agp.components.Text;
  8. public class MainAbilitySlice extends AbilitySlice implements Component.ClickedListener {
  9. Text text1 = null;
  10. @Override
  11. public void onStart(Intent intent) {
  12. super.onStart(intent);
  13. super.setUIContent(ResourceTable.Layout_ability_main);
  14. Button but1 = (Button)findComponentById(ResourceTable.Id_but1);
  15. text1 = (Text) findComponentById(ResourceTable.Id_text1);
  16. but1.setClickedListener(this);
  17. }
  18. @Override
  19. public void onActive() {
  20. super.onActive();
  21. }
  22. @Override
  23. public void onForeground(Intent intent) {
  24. super.onForeground(intent);
  25. }
  26. @Override
  27. public void onClick(Component component){
  28. Button but = (Button)component;
  29. text1.setText("Clicked");
  30. but.setText("Clicked");
  31. }
  32. }

第三种写法

匿名内部类

只能使用一次,且外部无法得知。

  1. package com.example.myapplication.slice;
  2. import com.example.myapplication.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. Button but1 = (Button)findComponentById(ResourceTable.Id_but1);
  13. but1.setClickedListener(new Component.ClickedListener() {
  14. @Override
  15. public void onClick(Component component) {
  16. Button but = (Button)component;
  17. but.setText("Clicked");
  18. }
  19. });
  20. }
  21. @Override
  22. public void onActive() {
  23. super.onActive();
  24. }
  25. @Override
  26. public void onForeground(Intent intent) {
  27. super.onForeground(intent);
  28. }
  29. }

第四种写法

方法引用

需要同接口方法一致,用引用的方法作为接口的实现

onClick

  1. package com.example.myapplication.slice;
  2. import com.example.myapplication.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. Button but1 = (Button)findComponentById(ResourceTable.Id_but1);
  13. but1.setClickedListener(this::onClick);
  14. }
  15. @Override
  16. public void onActive() {
  17. super.onActive();
  18. }
  19. @Override
  20. public void onForeground(Intent intent) {
  21. super.onForeground(intent);
  22. }
  23. public void onClick(Component componet){
  24. Button btu = (Button)componet;
  25. btu.setText("Clicked");
  26. }
  27. }

2.双击事件

同理

  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. <Text
  9. ohos:id="$+id:text1"
  10. ohos:height="match_content"
  11. ohos:width="match_content"
  12. ohos:text="请双击"
  13. ohos:text_size="100"
  14. />
  15. <Button
  16. ohos:id="$+id:but1"
  17. ohos:height="match_content"
  18. ohos:width="match_content"
  19. ohos:text="按钮"
  20. ohos:text_size="100"
  21. ohos:background_element="red"
  22. />
  23. </DirectionalLayout>
  1. package com.example.myapplication.slice;
  2. import com.example.myapplication.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. import ohos.agp.components.Text;
  8. public class MainAbilitySlice extends AbilitySlice implements Component.DoubleClickedListener {
  9. Text text1 = null;
  10. @Override
  11. public void onStart(Intent intent) {
  12. super.onStart(intent);
  13. super.setUIContent(ResourceTable.Layout_ability_main);
  14. Button but1 = (Button)findComponentById(ResourceTable.Id_but1);
  15. text1 = (Text)findComponentById(ResourceTable.Id_text1);
  16. but1.setDoubleClickedListener(this);
  17. }
  18. @Override
  19. public void onActive() {
  20. super.onActive();
  21. }
  22. @Override
  23. public void onForeground(Intent intent) {
  24. super.onForeground(intent);
  25. }
  26. @Override
  27. public void onDoubleClick(Component component) {
  28. text1.setText("已双击");
  29. }
  30. }

3.长按事件

  1. package com.example.myapplication.slice;
  2. import com.example.myapplication.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. import ohos.agp.components.Text;
  8. public class MainAbilitySlice extends AbilitySlice implements Component.LongClickedListener {
  9. Text text1 = null;
  10. @Override
  11. public void onStart(Intent intent) {
  12. super.onStart(intent);
  13. super.setUIContent(ResourceTable.Layout_ability_main);
  14. Button but1 = (Button)findComponentById(ResourceTable.Id_but1);
  15. text1 = (Text)findComponentById(ResourceTable.Id_text1);
  16. but1.setLongClickedListener(this);
  17. //but1.setLongClickable(false);在绑定事件后,通过该函数可为其能否长按属性进行改变
  18. }
  19. @Override
  20. public void onActive() {
  21. super.onActive();
  22. }
  23. @Override
  24. public void onForeground(Intent intent) {
  25. super.onForeground(intent);
  26. }
  27. @Override
  28. public void onLongClicked(Component component) {
  29. text1.setText("已长按");
  30. }
  31. }

4.滑动事件

需要知道按下、移动、松开,即:按下位置、抬起位置

滑动事件获取

我们现在为屏幕滑动事件进行获取

需要先为布局函数准备一个ID

  1. package com.example.myapplication.slice;
  2. import com.example.myapplication.ResourceTable;
  3. import ohos.aafwk.ability.AbilitySlice;
  4. import ohos.aafwk.content.Intent;
  5. import ohos.agp.components.*;
  6. import ohos.multimodalinput.event.TouchEvent;
  7. public class MainAbilitySlice extends AbilitySlice implements Component.TouchEventListener {
  8. Text text1 = null;
  9. int count = 0;
  10. @Override
  11. public void onStart(Intent intent) {
  12. super.onStart(intent);
  13. super.setUIContent(ResourceTable.Layout_ability_main);
  14. //1.先找到整个的布局对象
  15. DirectionalLayout dl = (DirectionalLayout) findComponentById(ResourceTable.Id_d1);
  16. text1 = (Text) findComponentById(ResourceTable.Id_text1);
  17. //2.为整个布局添加滑动事件
  18. //在布局上滑动时,就会执行
  19. //在按下、移动、松开这个过程中,程序会不断执行 onTouchEvent方法
  20. dl.setTouchEventListener(this);
  21. }
  22. @Override
  23. public void onActive() {
  24. super.onActive();
  25. }
  26. @Override
  27. public void onForeground(Intent intent) {
  28. super.onForeground(intent);
  29. }
  30. @Override
  31. //参数一是滑动的组件(对象也是一个组件)
  32. //参数二是动作对象(按下,滑动,抬起)
  33. public boolean onTouchEvent(Component component, TouchEvent touchEvent) {
  34. int action = touchEvent.getAction();
  35. count++;
  36. switch(action){
  37. case TouchEvent.PRIMARY_POINT_DOWN:{
  38. text1.setText("已按下,\n调用次数:"+count);
  39. break;
  40. }
  41. case TouchEvent.POINT_MOVE:{
  42. text1.setText("在移动,\n调用次数:"+count);
  43. break;
  44. }
  45. case TouchEvent.PRIMARY_POINT_UP: {
  46. text1.setText("未按下,\n调用次数:"+count);
  47. break;
  48. }
  49. default: {
  50. throw new IllegalStateException("Unexpected value: " + action);
  51. }
  52. }
  53. return true;
  54. }
  55. }

手机的坐标系

以坐上角为原点,竖直向下为Y轴正轴,水平向右为X轴正轴,垂直于屏幕向外为Z轴正轴

  1. public boolean onTouchEvent(Component component, TouchEvent touchEvent) {
  2. int action = touchEvent.getAction();
  3. switch(action){
  4. case TouchEvent.PRIMARY_POINT_DOWN:{
  5. //鸿蒙支持多手指操作,0代表第一个手指
  6. MmiPoint point = touchEvent.getPointerPosition(0);
  7. float x = point.getX();
  8. float y = point.getY();
  9. text1.setText("("+x+","+y+")");
  10. break;
  11. }
  12. case TouchEvent.POINT_MOVE:{
  13. MmiPoint point = touchEvent.getPointerPosition(0);
  14. float x = point.getX();
  15. float y = point.getY();
  16. text1.setText("("+x+","+y+")");
  17. break;
  18. }
  19. case TouchEvent.PRIMARY_POINT_UP: {
  20. MmiPoint point = touchEvent.getPointerPosition(0);
  21. float x = point.getX();
  22. float y = point.getY();
  23. text1.setText("("+x+","+y+")");
  24. break;
  25. }
  26. default: {
  27. throw new IllegalStateException("Unexpected value: " + action);
  28. }
  29. }
  30. return true;
  31. }

通过获取点击点和移动点,比较其坐标变化,获取滑动方向

右滑(手指向右移动):Y轴不变,X轴变大

左滑:Y轴不变,X轴变小

上滑:X轴不变,Y轴变小

下滑:X轴不变,Y轴变大

  1. package com.example.myapplication.slice;
  2. import com.example.myapplication.ResourceTable;
  3. import ohos.aafwk.ability.AbilitySlice;
  4. import ohos.aafwk.content.Intent;
  5. import ohos.agp.components.*;
  6. import ohos.multimodalinput.event.MmiPoint;
  7. import ohos.multimodalinput.event.TouchEvent;
  8. public class MainAbilitySlice extends AbilitySlice implements Component.TouchEventListener {
  9. Text text1 = null;
  10. float startx = 0;
  11. float starty = 0;
  12. @Override
  13. public void onStart(Intent intent) {
  14. super.onStart(intent);
  15. super.setUIContent(ResourceTable.Layout_ability_main);
  16. //1.先找到整个的布局对象
  17. DirectionalLayout dl = (DirectionalLayout) findComponentById(ResourceTable.Id_d1);
  18. text1 = (Text) findComponentById(ResourceTable.Id_text1);
  19. //2.为整个布局添加滑动事件
  20. //在布局上滑动时,就会执行
  21. //在按下、移动、松开这个过程中,程序会不断执行 onTouchEvent方法
  22. dl.setTouchEventListener(this);
  23. }
  24. @Override
  25. public void onActive() {
  26. super.onActive();
  27. }
  28. @Override
  29. public void onForeground(Intent intent) {
  30. super.onForeground(intent);
  31. }
  32. @Override
  33. //参数一是滑动的组件(对象也是一个组件)
  34. //参数二是动作对象(按下,滑动,抬起)
  35. public boolean onTouchEvent(Component component, TouchEvent touchEvent) {
  36. int action = touchEvent.getAction();
  37. switch(action){
  38. case TouchEvent.PRIMARY_POINT_DOWN:{
  39. //鸿蒙支持多手指操作,0代表第一个手指
  40. MmiPoint point = touchEvent.getPointerPosition(0);
  41. startx = point.getX();
  42. starty = point.getY();
  43. text1.setText("已点击");
  44. break;
  45. }
  46. case TouchEvent.POINT_MOVE:{
  47. break;
  48. }
  49. case TouchEvent.PRIMARY_POINT_UP: {
  50. MmiPoint point = touchEvent.getPointerPosition(0);
  51. float endx = point.getX();
  52. float endy = point.getY();
  53. if(endx>startx) {
  54. if(starty>endy){
  55. text1.setText("右上滑动");
  56. }
  57. else if(starty<endy){
  58. text1.setText("右下滑动");
  59. }
  60. else {
  61. text1.setText("右滑");
  62. }
  63. }
  64. else if(endx <startx){
  65. if(starty>endy){
  66. text1.setText("左上滑动");
  67. }
  68. else if(starty<endy){
  69. text1.setText("左下滑动");
  70. }
  71. else {
  72. text1.setText("左滑");
  73. }
  74. }
  75. else{
  76. if(starty>endy){
  77. text1.setText("上滑");
  78. }
  79. else if(starty<endy){
  80. text1.setText("下滑");
  81. }
  82. else {
  83. text1.setText("未滑动");
  84. }
  85. }
  86. break;
  87. }
  88. default: {
  89. throw new IllegalStateException("Unexpected value: " + action);
  90. }
  91. }
  92. return true;
  93. }
  94. }

返回值问题

为true时:表示所有的动作都会触发当前方法并执行对应代码

为false时:只有第一个动作会触发当前方法并执行对应代码,后续不再触发

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