四、代码示例
代码展示顺序 :
① Ability 配置文件 ;
② 主界面 Page Ability 代码 ;
③ 主界面 Page Ability 对应的 AbilitySlice 代码 ;
④ 主界面 Page Ability 对应的 AbilitySlice 对应的布局文件代码 ;
⑤ 跳转目标界面 Page Ability 代码 ;
⑥ 跳转目标界面 Page Ability 对应的 AbilitySlice 代码 ;
⑦ 跳转目标界面 Page Ability 对应的 AbilitySlice 对应的布局文件代码 ;
config.json 完整配置文件 : 主要是查看 Page Ability 的 action 是如何配置的 ;
{ "app": { "bundleName": "com.example.harmony", "vendor": "example", "version": { "code": 1, "name": "1.0" }, "apiVersion": { "compatible": 3, "target": 4, "releaseType": "Beta1" } }, "deviceConfig": {}, "module": { "package": "com.example.ability", "name": ".MyApplication", "deviceType": [ "phone" ], "distro": { "deliveryWithInstall": true, "moduleName": "ability", "moduleType": "feature" }, "abilities": [ { "orientation": "unspecified", "visible": true, "name": "com.example.ability.MainAbility", "icon": "$media:icon", "description": "$string:mainability_description", "label": "ability", "type": "page", "launchType": "standard" }, { "skills":[ { "actions":[ "ability.intent.MainAbility2" ] } ], "orientation": "unspecified", "visible": true, "name": "com.example.ability.MainAbility2", "icon": "$media:icon", "description": "$string:mainability_description", "label": "ability", "type": "page", "launchType": "standard" } ] } }
MainAbility 主界面 Page Ability :
package com.example.ability; import com.example.ability.slice.MainAbilitySlice; import ohos.aafwk.ability.Ability; import ohos.aafwk.content.Intent; public class MainAbility extends Ability { @Override public void onStart(Intent intent) { super.onStart(intent); super.setMainRoute(MainAbilitySlice.class.getName()); } }
MainAbilitySlice 显示内容 :
package com.example.ability.slice; import com.example.ability.ResourceTable; import ohos.aafwk.ability.AbilitySlice; import ohos.aafwk.content.Intent; 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); // 获取 XML 布局中的 Button 按钮 Button button = (Button) findComponentById(ResourceTable.Id_button); // 设置 Button 按钮点击事件 button.setClickedListener(new Component.ClickedListener() { @Override public void onClick(Component component) { // 初始化传递的 Intent 意图对象 Intent intent = new Intent(); // 设置跳转的目标 Page Ability 的 action 标识 intent.setAction("ability.intent.MainAbility2"); // 设置传递的数据 intent.setParam("DATA", 888); // 跳转到目标 Page Ability startAbility(intent); } }); } @Override public void onActive() { super.onActive(); } @Override public void onForeground(Intent intent) { super.onForeground(intent); } }
MainAbilitySlice 布局文件 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: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>
MainAbility2 跳转目标界面 Page Ability :
package com.example.ability; import com.example.ability.slice.MainAbilitySlice2; import ohos.aafwk.ability.Ability; import ohos.aafwk.content.Intent; public class MainAbility2 extends Ability { @Override public void onStart(Intent intent) { super.onStart(intent); super.setMainRoute(MainAbilitySlice2.class.getName()); } }
MainAbilitySlice2 显示内容 :
package com.example.ability.slice; import com.example.ability.ResourceTable; import ohos.aafwk.ability.AbilitySlice; import ohos.aafwk.content.Intent; 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 布局文件 ability_main2.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 页面 " 按钮 ;