鸿蒙OS 实现页面跳转

2020-09-15 10:38 更新
  1. 打开第一个页面的“MainAbilitySlice.java”文件,重写onStart()方法添加按钮的响应逻辑,实现点击按钮跳转到下一页,示例代码如下:

  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.aafwk.content.Operation;
  6. import ohos.agp.components.*;
  7. public class MainAbilitySlice extends AbilitySlice {
  8. @Override
  9. public void onStart(Intent intent) {
  10. super.onStart(intent);
  11. super.setUIContent(ResourceTable.Layout_main_layout);
  12. Button button = (Button) findComponentById(ResourceTable.Id_button);
  13. if (button != null) {
  14. // 为按钮设置点击回调
  15. button.setClickedListener(new Component.ClickedListener() {
  16. @Override
  17. public void onClick(Component component) {
  18. Intent secondIntent = new Intent();
  19. // 指定待启动FA的bundleName和abilityName
  20. Operation operation = new Intent.OperationBuilder()
  21. .withDeviceId("")
  22. .withBundleName("com.example.myapplication")
  23. .withAbilityName("com.example.myapplication.SecondAbility")
  24. .build();
  25. secondIntent.setOperation(operation);
  26. startAbility(secondIntent); // 通过AbilitySlice的startAbility接口实现启动另一个页面
  27. }
  28. });
  29. }
  30. }
  31. @Override
  32. public void onActive() {
  33. super.onActive();
  34. }
  35. @Override
  36. public void onForeground(Intent intent) {
  37. super.onForeground(intent);
  38. }
  39. }

  1. 再次运行项目,效果如图所示:

点击放大

以上内容是否对您有帮助:
在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号