赞
踩
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"
- ohos:alignment="center"
- >
- <Button
- ohos:id="$+id:but1"
- ohos:height="match_content"
- ohos:width="match_content"
- ohos:background_element="#FF00FF"
- ohos:layout_alignment="horizontal_center"
- ohos:text="无参无返回的跳转"
- ohos:text_size="40vp"
- />
- <Button
- ohos:id="$+id:but2"
- ohos:height="match_content"
- ohos:width="match_content"
- ohos:background_element="#FF00FF"
- ohos:layout_alignment="horizontal_center"
- ohos:text="有参无返回的跳转"
- ohos:text_size="40vp"
- />
- <Button
- ohos:id="$+id:but3"
- ohos:height="match_content"
- ohos:width="match_content"
- ohos:background_element="#FF00FF"
- ohos:layout_alignment="horizontal_center"
- ohos:text="有参有返回的跳转"
- ohos:text_size="40vp"
- />
-
- </DirectionalLayout>
abilityslice1.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:text1"
- ohos:height="match_content"
- ohos:width="match_content"
- ohos:background_element="#FF00FF"
- ohos:text="无参无返回的跳转"
- ohos:text_size="40vp"
- />
- </DirectionalLayout>
abilityslice2.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:text2"
- ohos:height="match_content"
- ohos:width="match_content"
- ohos:background_element="#FF00FF"
- ohos:text="有参无返回的跳转"
- ohos:text_size="40vp"
- />
- </DirectionalLayout>
abilityslice3.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:text3"
- ohos:height="match_content"
- ohos:width="match_content"
- ohos:background_element="#FF00FF"
- ohos:text="有参右有返回的跳转"
- ohos:text_size="40vp"
- />
- </DirectionalLayout>
新建3个页面切片
为config添加动作(别名)
修改MainAbility
- package com.example.myapplication;
-
- import com.example.myapplication.slice.AbilitySlice1;
- import com.example.myapplication.slice.AbilitySlice2;
- import com.example.myapplication.slice.AbilitySlice3;
- import com.example.myapplication.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());
- //添加其他子页面
- super.addActionRoute("slice1",AbilitySlice1.class.getName());
- super.addActionRoute("slice2", AbilitySlice2.class.getName());
- super.addActionRoute("slice3",AbilitySlice3.class.getName());
- }
- }
MainAbilitySlice
- package com.example.myapplication.slice;
-
- import com.example.myapplication.ResourceTable;
- import ohos.aafwk.ability.AbilitySlice;
- import ohos.aafwk.content.Intent;
- import ohos.agp.components.Button;
- import ohos.agp.components.Component;
- import ohos.agp.window.dialog.ToastDialog;
-
- public class MainAbilitySlice extends AbilitySlice implements Component.ClickedListener{
- Button but1;
- Button but2;
- Button but3;
- @Override
- public void onStart(Intent intent) {
- super.onStart(intent);
- super.setUIContent(ResourceTable.Layout_ability_main);
- but1 = (Button)findComponentById(ResourceTable.Id_but1);
- but2 = (Button)findComponentById(ResourceTable.Id_but2);
- but3 = (Button)findComponentById(ResourceTable.Id_but3);
- but1.setClickedListener(this);
- but2.setClickedListener(this);
- but3.setClickedListener(this);
- }
-
- @Override
- public void onActive() {
- super.onActive();
- }
-
- @Override
- public void onForeground(Intent intent) {
- super.onForeground(intent);
- }
-
- //requestCode为请求码,用来确认当前结果过是哪来的
- //resultIntent为结果意图,返回的意图对象
- @Override
- protected void onResult(int requestCode, Intent resultIntent) {
- super.onResult(requestCode, resultIntent);
- if(requestCode==200){
- //第二个参数为默认结果
- boolean result = resultIntent.getBooleanParam("result",false);
- ToastDialog td = new ToastDialog(this);
- if(result){
- td.setText("登录成功");
- }
- else{
- td.setText("登录失败");
- }
- td.setDuration(2000);
- td.show();
- }
- }
-
- @Override
- public void onClick(Component component) {
- //意图,即跳转时我们要做的事情
- //参数1.确定跳转时跳转到哪一个设备:operation
- //参数2.在跳转的时候是否需要带上参数:parameter
- //如果是在同一个Ability下,那么第一个参数可以不写
- //如果不需要参数,则也可以不写第二个参数
-
-
- if(component == but1){
- //无参无返回的跳转
- //创建意图对象
- Intent i = new Intent();
- //跳转
- present(new AbilitySlice1(),i);
-
- }
- else if(component == but2){
- //有参无返回的跳转
- Intent i = new Intent();
- i.setParam("username","zhangsan");
- i.setParam("password","123456");
- present(new AbilitySlice2(),i);
- }
- else if(component == but3){
- //有参有返回的跳转
- Intent i = new Intent();
- i.setParam("username","zhangsan");
- i.setParam("password","123456");
- //请求码自己设置
- presentForResult(new AbilitySlice3(),i,200);
- }
- }
- }
AbilitySlice1
- package com.example.myapplication.slice;
-
- import com.example.myapplication.ResourceTable;
- import ohos.aafwk.ability.AbilitySlice;
- import ohos.aafwk.content.Intent;
-
- public class AbilitySlice1 extends AbilitySlice {
- @Override
- public void onStart(Intent intent) {
- super.onStart(intent);
- super.setUIContent(ResourceTable.Layout_abilityslice1);
- }
-
- @Override
- public void onActive() {
- super.onActive();
- }
-
- @Override
- public void onForeground(Intent intent) {
- super.onForeground(intent);
- }
- }
AbilitySlice2
- package com.example.myapplication.slice;
-
- import com.example.myapplication.ResourceTable;
- import ohos.aafwk.ability.AbilitySlice;
- import ohos.aafwk.content.Intent;
- import ohos.agp.components.Text;
-
- public class AbilitySlice2 extends AbilitySlice {
- @Override
- public void onStart(Intent intent) {
- super.onStart(intent);
- super.setUIContent(ResourceTable.Layout_abilityslice2);
- String unm = intent.getStringParam("username");
- String pwd = intent.getStringParam("password");
- Text text = (Text)findComponentById(ResourceTable.Id_text2);
- text.setText("你好,"+unm+"!密码是:"+pwd);
- }
-
- @Override
- public void onActive() {
- super.onActive();
- }
-
- @Override
- public void onForeground(Intent intent) {
- super.onForeground(intent);
- }
- }
AbilitySlice3
- 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 AbilitySlice3 extends AbilitySlice{
- @Override
- public void onStart(Intent intent) {
- super.onStart(intent);
- super.setUIContent(ResourceTable.Layout_abilityslice3);
- Text text = (Text)findComponentById(ResourceTable.Id_text3);
- String unm = intent.getStringParam("username");
- String pwd = intent.getStringParam("password");
- Intent i = new Intent();
- if(pwd=="123456") {
- text.setText("你好,"+unm+"即将跳转");
- i.setParam("result",true);
- //返回意图
- setResult(i);
- //关闭当前页面
- terminate();
- }
- else{
- i.setParam("result",false);
- //返回意图
- setResult(i);
- //关闭当前页面
- terminate();
- }
-
- }
-
- @Override
- public void onActive() {
- super.onActive();
- }
-
- @Override
- public void onForeground(Intent intent) {
- super.onForeground(intent);
- }
-
- }
搭配一部分页面切片间的跳转方式即:
创建PageAbility(不要勾选LaunchAbility:指的是APP主页面)
准备两个Ability,三个切片(两个主切片+一个子切片)
别忘了将子切片绑定在第二个主页面上,同时为其注册一个行动
- 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.Button;
- import ohos.agp.components.Component;
-
-
- public class MainAbilitySlice extends AbilitySlice implements Component.ClickedListener{
- Button but1;
- Button but2;
- @Override
- public void onStart(Intent intent) {
- super.onStart(intent);
- super.setUIContent(ResourceTable.Layout_ability_main);
- but1 = (Button)findComponentById(ResourceTable.Id_but1);
- but2 = (Button)findComponentById(ResourceTable.Id_but2);
- but1.setClickedListener(this);
- but2.setClickedListener(this);
- }
-
- @Override
- public void onActive() {
- super.onActive();
- }
-
- @Override
- public void onForeground(Intent intent) {
- super.onForeground(intent);
- }
-
-
- @Override
- public void onClick(Component component) {
- if(component == but1){
- Intent i = new Intent();
- //OperationBuilder拼接参数
- //withDeviceId跳转的机器ID,“”表示本机
- //withBundleName哪一个应用上(config.json中的BundleName)
- //withAbilityName哪一个页面上(config.json中的name)
- //此时默认跳转到页面的主切面
- Operation oper = new Intent.OperationBuilder()
- .withDeviceId("")
- .withBundleName("com.example.myapplication.hmservice")
- .withAbilityName("com.example.myapplication.MainAbility2")
- .build();
- i.setOperation(oper);
- //i.setParam("test","test");
- startAbility(i);
- //startAbilityForResult(i,200);
- }
- else if(component == but2){
- //由于我们这里在config.json中注册了的,且在同一个APP中
- //因此可以用省略写法,只写有用参数,即withAction
- Intent i = new Intent();
- Operation oper = new Intent.OperationBuilder()
- .withAction("slice2")
- .build();
- i.setOperation(oper);
- startAbility(i);
- }
- }
- }
过程:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。