11.HarmonyOS鸿蒙app_page的显示跳转方法

11.HarmonyOS鸿蒙app_page的显示跳转方法,text文本触发点击事件

使用Intent和Operation对象

创建新项目后,再创建secondPageAbility

ability_main.xml

复制代码
<?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_main"
        ohos:height="match_parent"
        ohos:width="match_content"
        ohos:background_element="$graphic:background_ability_main"
        ohos:layout_alignment="horizontal_center"
        ohos:text="$string:main_title1"
        ohos:text_size="40vp"
        />

</DirectionalLayout>

Secondability_main.xml

复制代码
<?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_second"
        ohos:height="match_parent"
        ohos:width="match_content"
        ohos:background_element="#9090FF"
        ohos:layout_alignment="horizontal_center"
        ohos:text="$string:second_title"
        ohos:text_size="40vp"
        />

</DirectionalLayout>

MainAbilitySlice.java

复制代码
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.Component;
import ohos.agp.components.Text;

public class MainAbilitySlice extends AbilitySlice {
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        Text text = (Text) findComponentById(ResourceTable.Id_text_main);
        text.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                Intent _intent = new Intent();
                //创建Operation对象
                Operation operation = new Intent.OperationBuilder().withDeviceId("")
                        .withBundleName("com.example.myapplication")
                        .withAbilityName("com.example.myapplication.SecondPageAbility")
                        .build();
                _intent.setOperation(operation);
                //启动ability
                startAbility(_intent);
            }
        });
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}

SecondPageAbilitySlice.java

复制代码
//结束当前的Ability
terminateAbility();
复制代码
package com.example.myapplication.slice;

import com.example.myapplication.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.Text;

public class SecondPageAbilitySlice extends AbilitySlice {
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_Secondability_main);
        Text text = (Text) findComponentById(ResourceTable.Id_text_second);
        text.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                //结束当前的Ability
                terminateAbility();
            }
        });
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}

string的配置

相关推荐
Xxtaoaooo18 分钟前
React Native 跨平台鸿蒙开发实战:性能优化与启动加速技巧
react native·react.js·harmonyos
Easonmax28 分钟前
基础入门 React Native 鸿蒙跨平台开发:有趣编程——模拟手电筒
react native·react.js·harmonyos
Xxtaoaooo41 分钟前
React Native跨平台鸿蒙开发实战:JS 与 ArkTS Native的通信机制详解
javascript·react native·harmonyos
Easonmax43 分钟前
基础入门 React Native 鸿蒙跨平台开发:有趣编程——模拟一个简单的空调遥控器
react native·react.js·harmonyos
行走的鱼儿1 小时前
鸿蒙HarmonyOS随笔
华为·web·harmonyos·arkts·arkdata·dev eco·hmos
Xxtaoaooo1 小时前
React Native 跨平台鸿蒙开发实战:网络请求与鸿蒙分布式能力集成
网络·react native·harmonyos
Easonmax1 小时前
基础入门 React Native 鸿蒙跨平台开发:有趣编程——模拟洗衣机
react native·react.js·harmonyos
zilikew1 小时前
Flutter框架跨平台鸿蒙开发——读书笔记工具APP的开发流程
flutter·华为·harmonyos·鸿蒙
yumgpkpm1 小时前
在AI语言大模型时代 Cloudera CDP(华为CMP 鲲鹏版)对自有知识的保护
人工智能·hadoop·华为·zookeeper·spark·kafka
lbb 小魔仙1 小时前
【Harmonyos】开源鸿蒙跨平台训练营DAY6:为首页轮播图渲染(及常见问题与方法)
华为·开源·harmonyos