日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

HarmonyOS 頁面跳轉

作者:安果移不動 更新時間: 2022-05-12 編程語言

修改默認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

?

原文鏈接:https://blog.csdn.net/mp624183768/article/details/124368945

欄目分類
最近更新