Intent数据传递(单向) 原创

CHXI29
发布于 2021-5-27 20:37
浏览
0收藏

基本概念

Intent是对象之间传递信息的载体。例如,当MainAbility需要启动另一个SecondAbility时,或者一个MainAbilitySlice需要导航到另一个SecondAbilitySlice时,可以通过Intent指定启动的目标同时携带相关数据。

Intent数据传递(单向)-鸿蒙开发者社区

此图描述了 数据单向传递 与 双向传递时 需要调用到的方法。

单向数据传递案例演示(使用官网快速入门案例)

官网案例链接 https://developer.harmonyos.com/cn/docs/documentation/doc-guides/start-first-page-0000000000038014

此案例演示的是 在MainAbilitySlice的intent中存入数据 跳转到SecondAbilitySlice, 并展示到SecondAbilitySlice 的text中

MainAbilitySlice具体代码

public class MainAbilitySlice extends AbilitySlice {
    HiLogLabel label =new HiLogLabel(HiLog.LOG_APP,0xFFFFF,"CHXI");
    @Override
    public void onStart(Intent intent) {

        super.setUIContent(ResourceTable.Layout_ability_main);
        Button button = (Button) findComponentById(ResourceTable.Id_button);
        button.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                Intent myIntent = new Intent();
                myIntent.setParam("CHXI", "29");
                //导航到SecondAbilitySlice,并且携带myIntent
                present(new SecondAbilitySlice(), myIntent);
            }
        });
    }
}

SecondAbilitySlice具体代码

 HiLogLabel label = new HiLogLabel(HiLog.LOG_APP,0xFFFFF,"CHXI29");
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);

        // 声明布局
        DependentLayout myLayout = new DependentLayout(this);

        // 设置布局宽高
        myLayout.setWidth(DependentLayout.LayoutConfig.MATCH_PARENT);
        myLayout.setHeight(DependentLayout.LayoutConfig.MATCH_PARENT);

        // 设置布局背景为白色
        ShapeElement background = new ShapeElement();
        background.setRgbColor(new RgbColor(255, 255, 255));
        myLayout.setBackground(background);

        // 创建一个文本
        Text text = new Text(this);
        text.setText("Hi there ~");
        text.setWidth(DependentLayout.LayoutConfig.MATCH_PARENT);
        text.setTextSize(70);
        text.setTextColor(Color.BLACK);

        //设置按钮背景色
        ShapeElement backgroundbutton = new ShapeElement();
        backgroundbutton.setRgbColor(new RgbColor(0,200,200));
        //创建一个buttonon
        Button button = new Button(this);
        button.setBackground(backgroundbutton);
        button.setText("按钮");
        button.setTextSize(100, Text.TextSizeType.PX);
        //添加单击事件
        button.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                //根据key获取myIntent里面value 并展示到text中
                String str = intent.getStringParam("CHXI");
                text.setText(str);
            }
        });


        // 设置文本的布局
        DependentLayout.LayoutConfig textConfig = new DependentLayout.LayoutConfig(DependentLayout.LayoutConfig.MATCH_CONTENT, DependentLayout.LayoutConfig.MATCH_CONTENT);
        textConfig.addRule(DependentLayout.LayoutConfig.CENTER_IN_PARENT);
        DependentLayout.LayoutConfig butconfig  = new DependentLayout.LayoutConfig(DependentLayout.LayoutConfig.MATCH_CONTENT,DependentLayout.LayoutConfig.MATCH_CONTENT);
        text.setLayoutConfig(textConfig);
        //设置按钮布局
        butconfig.setMarginLeft(100);
        butconfig.setMarginTop(1300);
        button.setLayoutConfig(butconfig);

        myLayout.addComponent(button);
        myLayout.addComponent(text);

        super.setUIContent(myLayout);
    }

实验截图

1、启动后主页面

Intent数据传递(单向)-鸿蒙开发者社区

2、点击Next后跳转到SecondAbilitySlice

Intent数据传递(单向)-鸿蒙开发者社区

3、点击【按钮】获取到MainAbilitySlice中存放的值 ‘29’,并展示到text中

Intent数据传递(单向)-鸿蒙开发者社区

–后续继续完善

©著作权归作者所有,如需转载,请注明出处,否则将追究法律责任
标签
已于2021-5-27 20:51:40修改
1
收藏
回复
举报
1条回复
按时间正序
/
按时间倒序
鸿蒙张荣超
鸿蒙张荣超

赞赞赞!

回复
2021-5-28 07:49:49
回复
    相关推荐