鸿蒙开发基础(三)跳转、传值及回传值

Slice之间的跳转

present(new MainAbilitySlice2(), intent1);

传值

Slice1:

text.setText("Hello World1");
text.setClickedListener(new Component.ClickedListener() {
    @Override
    public void onClick(Component component) {
        Intent intent1 = new Intent();
        intent1.setParam("user", "cnyl");
        present(new MainAbilitySlice2(), intent1);
    }
});

Slice2:

String user = "";
if (intent!=null){
    user = intent.getStringParam("user");
}
text.setText("Hello World2" + user);

回传值

MainAbilitySlice

public class MainAbilitySlice extends AbilitySlice {

    private DirectionalLayout myLayout = new DirectionalLayout(this);
    Text text;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        LayoutConfig config = new LayoutConfig(LayoutConfig.MATCH_PARENT, LayoutConfig.MATCH_PARENT);
        myLayout.setLayoutConfig(config);
        ShapeElement element = new ShapeElement();
        element.setRgbColor(new RgbColor(255, 255, 255));
        myLayout.setBackground(element);

        text = new Text(this);
        text.setLayoutConfig(config);
        text.setText("这是S1");
        text.setTextColor(new Color(0xFF000000));
        text.setTextSize(50);
        text.setTextAlignment(TextAlignment.CENTER);
        text.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                Intent intent1 = new Intent();
                intent1.setParam("user", "cnyl");
                presentForResult(new MainAbilitySlice2(), intent1,123);
            }
        });
        myLayout.addComponent(text);
        super.setUIContent(myLayout);
    }

    @Override
    protected void onResult(int requestCode, Intent resultIntent) {     //在接到结果的时候会调用
        super.onResult(requestCode, resultIntent);
        if(requestCode==123){
            String pwd = resultIntent.getStringParam("pwd");
            text.setText("回到S1收到了来自S2的pwd:" + pwd);
        }
    }

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

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

MainAbilitySlice2

public class MainAbilitySlice2 extends AbilitySlice {

    private DirectionalLayout myLayout = new DirectionalLayout(this);

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        LayoutConfig config = new LayoutConfig(LayoutConfig.MATCH_PARENT, LayoutConfig.MATCH_PARENT);
        myLayout.setLayoutConfig(config);
        ShapeElement element = new ShapeElement();
        element.setRgbColor(new RgbColor(255, 255, 255));
        myLayout.setBackground(element);

        String user = "";
        if (intent!=null){
            user = intent.getStringParam("user");
        }

        Text text = new Text(this);
        text.setLayoutConfig(config);
        text.setText("在S2收到了来自S1的user:" + user);
        text.setTextColor(new Color(0xFF000000));
        text.setTextSize(50);
        text.setTextAlignment(TextAlignment.CENTER);
        text.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                Intent intent1 = new Intent();
                intent1.setParam("pwd", "123456");
                setResult(intent1);
                terminate();
            }
        });
        myLayout.addComponent(text);
        super.setUIContent(myLayout);
    }

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

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

Ability之间的跳转

MainAbilitySlice

public class MainAbilitySlice extends AbilitySlice {

    private DirectionalLayout myLayout = new DirectionalLayout(this);

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        LayoutConfig config = new LayoutConfig(LayoutConfig.MATCH_PARENT, LayoutConfig.MATCH_PARENT);
        myLayout.setLayoutConfig(config);
        ShapeElement element = new ShapeElement();
        element.setRgbColor(new RgbColor(255, 255, 255));
        myLayout.setBackground(element);

        Text text = new Text(this);
        text.setLayoutConfig(config);
        text.setText("Hello World1");
        text.setTextColor(new Color(0xFF000000));
        text.setTextSize(150);
        text.setTextAlignment(TextAlignment.CENTER);
        text.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                Intent intent = new Intent();

            // 通过Intent中的OperationBuilder类构造operation对象,指定设备标识(空串表示当前设备)、应用包名、Ability名称
                Operation operation = new Intent.OperationBuilder()
                        .withDeviceId("")
                        .withBundleName("com.example.test")
                        .withAbilityName("com.example.test.CNYLAbility")
                        .build();

            // 把operation设置到intent中
                intent.setOperation(operation);
                startAbility(intent);
            }
        });
        myLayout.addComponent(text);
        super.setUIContent(myLayout);
    }

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

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

CNYLAbilitySlice

可以简化

Intent intent = new Intent();
Operation operation = new Intent.OperationBuilder()
    .withBundleName("com.example.test")
    .withAbilityName(".CNYLAbility")
    .build();
intent.setOperation(operation);
startAbility(intent);

跳转到另一个Ability中,指定的Slice

MainAbilitySlice

public class MainAbilitySlice extends AbilitySlice {

    private DirectionalLayout myLayout = new DirectionalLayout(this);

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        LayoutConfig config = new LayoutConfig(LayoutConfig.MATCH_PARENT, LayoutConfig.MATCH_PARENT);
        myLayout.setLayoutConfig(config);
        ShapeElement element = new ShapeElement();
        element.setRgbColor(new RgbColor(255, 255, 255));
        myLayout.setBackground(element);

        Text text = new Text(this);
        text.setLayoutConfig(config);
        text.setText("Hello World1");
        text.setTextColor(new Color(0xFF000000));
        text.setTextSize(150);
        text.setTextAlignment(TextAlignment.CENTER);
        text.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                Intent intent = new Intent();
                intent.setAction("cnyl");
                startAbility(intent);
            }
        });
        myLayout.addComponent(text);
        super.setUIContent(myLayout);
    }

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

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

config.json

    "abilities": [
      {
        "skills": [
          {
            "entities": [
              "entity.system.home"
            ],
            "actions": [
              "action.system.home"
            ]
          }
        ],
        "orientation": "landscape",
        "formEnabled": false,
        "name": "com.example.test6.MainAbility",
        "icon": "$media:icon",
        "description": "$string:mainability_description",
        "label": "Test6",
        "type": "page",
        "launchType": "standard"
      },
      {
        "skills": [
          {
            "actions": [
              "cnyl"
            ]
          }
        ],
        "orientation": "landscape",
        "formEnabled": false,
        "name": "com.example.test6.CNYLAbility",
        "icon": "$media:icon",
        "description": "$string:cnylability_description",
        "label": "entry",
        "type": "page",
        "launchType": "standard"
      }
    ]

CNYLAbility

public class CNYLAbility extends Ability {
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setMainRoute(CNYLAbilitySlice.class.getName());

        addActionRoute("cnyl", "com.example.test6.slice.CNYLAbilitySlice2");
    }
}

Copy Reference --> "com.example.test6.slice.CNYLAbilitySlice2"
MainAbilitySlice > config.json > CNYLAbility > CNYLAbilitySlice2

posted @ 2020-10-27 23:14  颉颃  阅读(2142)  评论(0编辑  收藏  举报