> 文档中心 > HarmonyOS 页面跳转

HarmonyOS 页面跳转

修改默认Main的布局

        
match_content 是不是有人想起来了 wrap_content

ui效果如下

新建页面2

 

 取名

SecondAbility

就好

 修改代码 我们通过代码的方式创建布局

package com.example.helloworld.slice;import com.example.helloworld.ResourceTable;import ohos.aafwk.ability.AbilitySlice;import ohos.aafwk.content.Intent;import ohos.agp.components.DirectionalLayout;import ohos.agp.components.Text;import ohos.agp.utils.Color;public class SecondAbilitySlice extends AbilitySlice {    @Override    public void onStart(Intent intent) { super.onStart(intent);// super.setUIContent(ResourceTable.Layout_ability_second); //创建布局对象 DirectionalLayout directionalLayout = new DirectionalLayout(this); Text text = new Text(this); text.setText("第二个页面"); text.setTextSize(55); text.setTextColor(Color.BLUE); directionalLayout.addComponent(text); setUIContent(directionalLayout);    }    @Override    public void onActive() { super.onActive();    }    @Override    public void onForeground(Intent intent) { super.onForeground(intent);    }}
MainAbilitySlice

修改下

跳转核心代码来咯

package com.example.helloworld.slice;import com.example.helloworld.ResourceTable;import ohos.aafwk.ability.AbilitySlice;import ohos.aafwk.content.Intent;import ohos.aafwk.content.Operation;import ohos.agp.components.Button;import ohos.agp.components.Component;public class MainAbilitySlice extends AbilitySlice {    @Override    public void onStart(Intent intent) { super.onStart(intent); super.setUIContent(ResourceTable.Layout_ability_main); Button btn1 = findComponentById(ResourceTable.Id_btn1); btn1.setClickedListener(new Component.ClickedListener() {     @Override     public void onClick(Component component) {  Intent intent = new Intent();  //withDeviceId 跳转到那个设备上,如果传递一个没有内容的字符串 标识跳转本机  //withBundleName 跳转那个应用上面  //withAbilityName 要跳转的页面  Operation build = new Intent.OperationBuilder()   .withDeviceId("")   .withBundleName("com.example.helloworld")   .withAbilityName(".SecondAbility")   .build();  intent.setOperation(build);  startAbility(intent);     } });    }    @Override    public void onActive() { super.onActive();    }    @Override    public void onForeground(Intent intent) { super.onForeground(intent);    }}

Very ugly, but useful