Android开发之正反传值

Android开发之正反传值

第一定义,注意是在头部

 String fxstr="矢量地图";

    //1.反向传值
    ActivityResultLauncher<Intent> launcher = registerForActivityResult(
            new ActivityResultContracts.StartActivityForResult(),
            result -> {
                if (result.getResultCode() == RESULT_OK) {
                    //拿到返回的数据
                     fxstr = result.getData().getStringExtra("data");
                    Toast.makeText(MainActivity.this, fxstr, Toast.LENGTH_SHORT).show();
                }
            });

跳转

  //反向传值2
     // bt_lay = (ImageView) findViewById(R.id.sa_lay);//2
      findViewById(R.id.sa_lay).setOnClickListener(view -> {
          //使用
          Intent intent2 = new Intent(this,MapLaysetActivity.class);
          intent2.putExtra("name",fxstr);
          launcher.launch(intent2);
      });

回传


void  fxvalue(){

    //请求返回结果
    ActivityResultLauncher<Intent> launcher = registerForActivityResult(
            new ActivityResultContracts.StartActivityForResult(),
            result -> {
                if (result.getResultCode() == RESULT_OK) {
                    //拿到返回的数据
                    String str = result.getData().getStringExtra("data");
                    Toast.makeText(MainActivity.this, str, Toast.LENGTH_SHORT).show();
                }
            });

第二页面

//获取意图对象
intent = getIntent();
name = intent.getStringExtra("name");

============

name= radbtn.getText()+"";

Intent intent = new Intent();
intent.putExtra("data", name);
setResult(RESULT_OK, intent);
finish();
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值