主要活动包括一些具有设定值的变量。我创建了一个子活动,其形式必须填充主活动中的数据,所以我猜数据在启动时必须传递给子活动。
有谁知道如何将变量值传递给主要活动的子活动?
谢谢!
答案 0 :(得分:20)
您可以在主要活动中使用此方法
Intent i = new Intent(this, YourMainClass.class);
i.putExtra("key", value);
然后在子活动中使用此方法获取值,通常在onCreate事件中
int value = getIntent().getExtras().getInt("key");
我希望这可以解决。
答案 1 :(得分:2)
这会在主要活动中发挥作用吗?
Intent i = new Intent(this, YourMainClass.class);
i.putExtra("key", value);
其次是:
String value = getIntent().getExtras().getString("key");
你可以添加多个“Extras”之类的东西吗?
i.putExtra("key", value1);
i.putExtra("key2", value2);
i.putExtra("key3", value3);
...谢谢
答案 2 :(得分:0)
试试这个会起作用:
activity1.class:
Intent i = new Intent(activity1.this,activity2.class);
Bundle b = new Bundle();
b.putString("name", "your value need to pass here");
i.putExtras(b);
startActivity(i);
activity2.class:
Bundle b = this.getIntent().getExtras();
String name = b.getString("name");
((TextView)findViewById(R.id.textView1)).setText(name);