Intent数据传递(双向)
头像 宋你一朵小红花 2021-05-28 11:12:10    发布
3077 浏览 22 点赞 18 收藏

单向双向的图解可以在单向数据传递中查看 https://harmonyos.51cto.com/posts/4824

展示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) {
                //希望SecondSlice返回结果 使用presentForResult('目标页面',intent,请求码(自定义))
                presentForResult(new SecondAbilitySlice(),intent,99);
            }
        });
    }
    @Override
    protected void onResult(int requestCode, Intent resultIntent) {
        HiLog.info(label, " 请求码---->" + requestCode);
        String secondStr = resultIntent.getStringParam("second");
        HiLog.info(label, " Second返回的数据---->" + secondStr);
    }
}


public class SecondAbilitySlice extends AbilitySlice {
    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);

		/*双向传递核心代码*/
        //创建一个Intent 并返回给MainAbility
        Intent secondIntent = new Intent();
        secondIntent.setParam("second","two");
        setResult(secondIntent);

        super.setUIContent(myLayout);


    }

}

案例演示结果

1、启动项目

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


2、点击【Next】跳转到SecondAbilitySlice页面

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


3、此时点击下方的【返回键】 注意:是屏幕底下的三角符号

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


此时在MainAbilitySlice的onResult()函数已经获取到信息 并且打印到HILOG中


©本站发布的所有内容,包括但不限于文字、图片、音频、视频、图表、标志、标识、广告、商标、商号、域名、软件、程序等,除特别标明外,均来源于网络或用户投稿,版权归原作者或原出处所有。我们致力于保护原作者版权,若涉及版权问题,请及时联系我们进行处理。
分类
HarmonyOS
头像

宋你一朵小红花

我还没有写个人简介......

881

帖子

0

提问

1679

粉丝

关注
热门推荐
地址:北京市朝阳区北三环东路三元桥曙光西里甲1号第三置业A座1508室 商务内容合作QQ:2291221 电话:13391790444或(010)62178877
版权所有:电脑商情信息服务集团 北京赢邦策略咨询有限责任公司
声明:本媒体部分图片、文章来源于网络,版权归原作者所有,我司致力于保护作者版权,如有侵权,请与我司联系删除
京ICP备:2022009079号-2
京公网安备:11010502051901号
ICP证:京B2-20230255