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();

猜你喜欢

转载自blog.csdn.net/wushijun5200/article/details/129890168