实现页面跳转

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

    package com.example.myapplication.slice;
    
    import com.example.myapplication.ResourceTable;
    
    import ohos.aafwk.ability.AbilitySlice;
    import ohos.aafwk.content.Intent;
    import ohos.agp.components.Button;
    
    public class MainAbilitySlice extends AbilitySlice {
        @Override
        public void onStart(Intent intent) {
            super.onStart(intent);
            super.setUIContent(ResourceTable.Layout_ability_main);
            Button button = (Button) findComponentById(ResourceTable.Id_button);
    
            // 点击按钮跳转至第二个页面
            button.setClickedListener(listener -> present(new SecondAbilitySlice(), new Intent()));
        }
    }
    
  2. 再次运行项目,效果如下图所示。

    恭喜你,至此已成功完成HarmonyOS快速入门。

results matching ""

    No results matching ""