MainAbilitySlice 的 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:orientation="vertical"> <Text ohos:id="$+id:text" ohos:height="match_content" ohos:width="match_content" ohos:background_element="$graphic:background_ability_main" ohos:layout_alignment="horizontal_center" ohos:text="页面 1" ohos:text_size="50" /> <Button ohos:id="$+id:button" ohos:height="match_parent" ohos:width="match_content" ohos:layout_alignment="bottom|horizontal_center" ohos:text="跳转到 2 页面" ohos:text_size="50" /> </DirectionalLayout>
MainAbilitySlice2 源码 :
package com.example.harmony.slice; import com.example.harmony.ResourceTable; import ohos.aafwk.ability.AbilitySlice; import ohos.aafwk.content.Intent; import ohos.agp.components.Button; import ohos.agp.components.Component; import ohos.agp.components.Text; public class MainAbilitySlice2 extends AbilitySlice { @Override public void onStart(Intent intent) { super.onStart(intent); super.setUIContent(ResourceTable.Layout_ability_main2); // 获取 Text 文字显示组件 Text text = (Text) findComponentById(ResourceTable.Id_text); // 获取 Intent 中的数据 , 并显示到界面中 text.setText("页面 2 数据 : " + intent.getIntParam("DATA", 0)); } @Override public void onActive() { super.onActive(); } @Override public void onForeground(Intent intent) { super.onForeground(intent); } }
MainAbilitySlice2 的 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:orientation="vertical"> <Text ohos:id="$+id:text" ohos:height="match_content" ohos:width="match_content" ohos:background_element="$graphic:background_ability_main" ohos:layout_alignment="horizontal_center" ohos:text="页面 2" ohos:text_size="50" /> </DirectionalLayout>
三、运行结果
运行后初始主界面 :
点击 " 跳转到 2 页面 " 按钮 ;