赞
踩
MainAbilitySlice 继承了父类AbilitySlice ,该类是一个界面的类,
findComponentById是找到按钮的对象并返回。通过ID进行查找。其中Id_but1的类型是Id,名字是but1。findComponentById返回的是一个父类对象,需要向下转型。
ResourceTable是一个鸿蒙中界面的资源列表。如下图所示,每一个资源都有一个固定的ID。
后面的值是在内存中的地址值。通过ID可以找到按钮。
给按钮设置单击事件:setClickedListener。参数是单击按钮后要执行的操作。具体操作需要继承一个父类,重写onClick方法。
public class MainAbilitySlice extends AbilitySlice { @Override public void onStart(Intent intent) { super.onStart(intent); super.setUIContent(ResourceTable.Layout_ability_main); // 1.找到按钮 Button but1 = (Button) findComponentById(ResourceTable.Id_but1); // 2.给按钮绑定一个单击事件 setClickedListener需要传入一个接口实现类作为参数 but1.setClickedListener(new MyListener()); } @Override public void onActive() { super.onActive(); } @Override public void onForeground(Intent intent) { super.onForeground(intent); } } class MyListener implements ClickedListener{ boolean flag = false; @Override public void onClick(ohos.agp.components.Component component) { if (!flag) { Button btu = (Button) component; btu.setText("被点击"); flag = true; } else { Button btu = (Button) component; btu.setText("点我"); flag = false; } } }
public class MainAbilitySlice extends AbilitySlice implements ClickedListener { @Override public void onStart(Intent intent) { super.onStart(intent); super.setUIContent(ResourceTable.Layout_ability_main); // 1.找到按钮 Button but1 = (Button) findComponentById(ResourceTable.Id_but1); // 2.给按钮绑定一个单击事件 setClickedListener需要传入一个接口实现类作为参数 but1.setClickedListener(this); } @Override public void onActive() { super.onActive(); } @Override public void onForeground(Intent intent) { super.onForeground(intent); } boolean flag = false; @Override public void onClick(Component component) { if (!flag) { Button btu = (Button) component; btu.setText("被点击"); flag = true; } else { Button btu = (Button) component; btu.setText("点我"); flag = false; } } }
public class MainAbilitySlice extends AbilitySlice { @Override public void onStart(Intent intent) { super.onStart(intent); super.setUIContent(ResourceTable.Layout_ability_main); // 1.找到按钮 Button but1 = (Button) findComponentById(ResourceTable.Id_but1); // 2.给but1绑定的单击事件 but1.setClickedListener(new ClickedListener() { @Override public void onClick(Component component) { if (!flag) { Button btu = (Button) component; btu.setText("被点击"); flag = true; } else { Button btu = (Button) component; btu.setText("点我"); flag = false; } } }); } @Override public void onActive() { super.onActive(); } @Override public void onForeground(Intent intent) { super.onForeground(intent); } boolean flag = false; }
public class MainAbilitySlice extends AbilitySlice { @Override public void onStart(Intent intent) { super.onStart(intent); super.setUIContent(ResourceTable.Layout_ability_main); // 1.找到按钮 Button but1 = (Button) findComponentById(ResourceTable.Id_but1); // 2.给but1绑定的单击事件 but1.setClickedListener(this::onClick); } @Override public void onActive() { super.onActive(); } @Override public void onForeground(Intent intent) { super.onForeground(intent); } boolean flag = false; public void onClick(Component component) { if (!flag) { Button btu = (Button) component; btu.setText("被点击"); flag = true; } else { Button btu = (Button) component; btu.setText("点我"); flag = false; } } }
import ohos.agp.components.Component.ClickedListener; import ohos.agp.components.Component.DoubleClickedListener; import ohos.agp.components.Component.LongClickedListener; public class MainAbilitySlice extends AbilitySlice implements DoubleClickedListener, LongClickedListener{ boolean flag = false; @Override public void onStart(Intent intent) { super.onStart(intent); super.setUIContent(ResourceTable.Layout_ability_main); // 1.找到按钮 Button but1 = (Button) findComponentById(ResourceTable.Id_but1); Button but2 = (Button) findComponentById(ResourceTable.Id_but2); Button but3 = (Button) findComponentById(ResourceTable.Id_but3); // 2.给but1绑定的单击事件 but1.setClickedListener(this::onClick); but2.setDoubleClickedListener(this); but3.setLongClickedListener(this); } @Override public void onActive() { super.onActive(); } @Override public void onForeground(Intent intent) { super.onForeground(intent); } public void onClick(Component component) { if (!flag) { Button btu = (Button) component; btu.setText("被点击"); flag = true; } else { Button btu = (Button) component; btu.setText("点我"); flag = false; } } @Override public void onDoubleClick(Component component) { if (!flag) { Button btu = (Button) component; btu.setText("被点击"); flag = true; } else { Button btu = (Button) component; btu.setText("点我"); flag = false; } } @Override public void onLongClicked(Component component) { if (!flag) { Button btu = (Button) component; btu.setText("被长按"); flag = true; } else { Button btu = (Button) component; btu.setText("长按我"); flag = false; } }
主要有2点:判断按下的起点和终点位置,检测按键按下滑动松开动作
// 参数1:component表示滑动的那个组件(布局也是一种组件) // 参数2:touchEvent动作对象(按下,滑动,抬起) @Override public boolean onTouchEvent(Component component, TouchEvent touchEvent) { int action = touchEvent.getAction(); // 获取当前手指对屏幕进行的操作(按下,滑动,抬起) // 1 表示按下 // 2 表示松开 // 3 表示滑动/移动操作 if (action == TouchEvent.PRIMARY_POINT_DOWN) { text1.setText("按下"); } else if (action == TouchEvent.POINT_MOVE) { text1.setText("移动"); } else if (action == TouchEvent.PRIMARY_POINT_UP) { text1.setText("松开"); } return true; }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。