您的位置:58编程 > 鸿蒙os实现页面跳转怎么设置 鸿蒙OS 实现页面跳转

鸿蒙os实现页面跳转怎么设置 鸿蒙OS 实现页面跳转

2023-04-05 02:33 鸿蒙OS开发文档

鸿蒙os实现页面跳转怎么设置 鸿蒙OS 实现页面跳转

鸿蒙os实现页面跳转怎么设置 鸿蒙OS 实现页面跳转

鸿蒙os实现页面跳转怎么设置

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

   package com.example.myapplication.slice;

    
   import com.example.myapplication.ResourceTable;
   import ohos.aafwk.ability.AbilitySlice;
   import ohos.aafwk.content.Intent;
   import ohos.aafwk.content.Operation;
   import ohos.agp.components.*;

    
   public class MainAbilitySlice extends AbilitySlice {

    
       @Override
       public void onStart(Intent intent) {
           super.onStart(intent);
           super.setUIContent(ResourceTable.Layout_main_layout);
           Button button = (Button) findComponentById(ResourceTable.Id_button);

    
           if (button != null) {
               // 为按钮设置点击回调
               button.setClickedListener(new Component.ClickedListener() {
                   @Override
                   public void onClick(Component component) {
                   Intent secondIntent = new Intent();
                   // 指定待启动FA的bundleName和abilityName
                   Operation operation = new Intent.OperationBuilder()
                           .withDeviceId("")
                           .withBundleName("com.example.myapplication")
                           .withAbilityName("com.example.myapplication.SecondAbility")
                           .build();
                   secondIntent.setOperation(operation);
                   startAbility(secondIntent); // 通过AbilitySlice的startAbility接口实现启动另一个页面
                   }
               });
           }
       }

    
       @Override
       public void onActive() {
           super.onActive();
       }

    
       @Override
       public void onForeground(Intent intent) {
           super.onForeground(intent);
       }
   }

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

点击放大

阅读全文
以上是58编程为你收集整理的鸿蒙os实现页面跳转怎么设置 鸿蒙OS 实现页面跳转全部内容。
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。
相关文章
© 2024 58编程 58biancheng.com 版权所有 联系我们
桂ICP备12005667号-32 Powered by CMS