如何将变量传递给android中的另一个活动?

afr*_*eem 3 android android-activity

我在主要活动中有 2 个变量。我需要将这些变量值传递给下一个活动。我该怎么做?

   button.Click += delegate {
   var activity2 = new Intent (this, typeof(Activity2));
   activity2.PutExtra ("MyData", "Data from Activity1");
   StartActivity (activity2);
};
Run Code Online (Sandbox Code Playgroud)

Abu*_*kar 5

创建和意图对象并发送您的数据 throw putstring() 或 putExtra() 方法

 Intent intent = new Intent(this, YourClass.class);
 intent.putString("key1", var1);// if its string type
 Intent.putExtra("key2", var2);// if its int type
 startActivity(intent);
Run Code Online (Sandbox Code Playgroud)

在接收方

Intent intent = getIntent();
String var1 = intent.getStringExtra("key1");
int i = var2.getIntExtra("key2", 0);
Run Code Online (Sandbox Code Playgroud)