codecamp

鸿蒙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. 再次运行项目,效果如图所示:

点击放大

鸿蒙OS 创建另一个页面
鸿蒙OS Ability
温馨提示
下载编程狮App,免费阅读超1000+编程语言教程
取消
确定
目录

鸿蒙OS 开发

鸿蒙OS 术语

鸿蒙OS Java API参考

鸿蒙OS ohos.aafwk.ability

鸿蒙OS ohos.aafwk.abilityjet.activedata

鸿蒙OS ohos.aafwk.content

鸿蒙OS java.lang

鸿蒙OS java.Util

鸿蒙OS java.Util class

鸿蒙OS ohos.data.dataability

鸿蒙OS ohos.data.dataability class

鸿蒙OS ohos.agp.components

鸿蒙OS ohos.agp.components interface

鸿蒙OS ohos.agp.components class

鸿蒙OS ohos.global.configuration

鸿蒙OS java.io

鸿蒙OS ohos.data.resultset

鸿蒙OS ohos.data.resultset interface

关闭

MIP.setData({ 'pageTheme' : getCookie('pageTheme') || {'day':true, 'night':false}, 'pageFontSize' : getCookie('pageFontSize') || 20 }); MIP.watch('pageTheme', function(newValue){ setCookie('pageTheme', JSON.stringify(newValue)) }); MIP.watch('pageFontSize', function(newValue){ setCookie('pageFontSize', newValue) }); function setCookie(name, value){ var days = 1; var exp = new Date(); exp.setTime(exp.getTime() + days*24*60*60*1000); document.cookie = name + '=' + value + ';expires=' + exp.toUTCString(); } function getCookie(name){ var reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)'); return document.cookie.match(reg) ? JSON.parse(document.cookie.match(reg)[2]) : null; }