HarmonyOS 页面跳转

修改默认Main的布局

<?xml version="1.0" encoding="utf-8"?>
<DirectionalLayout
    xmlns:ohos="http://schemas.huawei.com/res/ohos"
    ohos:height="match_parent"
    ohos:width="match_parent"
    ohos:alignment="center"
    ohos:orientation="vertical">

    <Text
        ohos:id="$+id:text_helloworld"
        ohos:height="match_content"
        ohos:width="match_content"
        ohos:background_element="$graphic:background_ability_main"
        ohos:layout_alignment="horizontal_center"
        ohos:text="第一个页面"
        ohos:text_size="40vp"
        />

    <Button
        ohos:id="$+id:btn1"
        ohos:height="match_content"
        ohos:width="match_parent"
        ohos:background_element="red"
        ohos:text="跳转下一个页面"
        ohos:text_size="40fp"/>

</DirectionalLayout>
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

修改下

扫描二维码关注公众号,回复: 13994679 查看本文章

跳转核心代码来咯

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

猜你喜欢

转载自blog.csdn.net/mp624183768/article/details/124368945