2009-07-02 56 views
11

主要活动包括一些具有设定值的变量。我创建了一个子活动,其形式必须填充来自主活动的数据,所以我猜数据必须在启动时传递给子活动。Android:如何将数据传递给子活动?

有谁知道如何将变量值传递给主活动的子活动?

谢谢!

回答

20

您可以在您的主要活动使用此方法

Intent i = new Intent(this, YourMainClass.class); 
i.putExtra("key", value); 

末,然后在子活动用这种方法得到的值,一般在onCreate事件

int value = getIntent().getExtras().getInt("key"); 

我希望这hepls。

2

这项工作在主要活动中吗?

Intent i = new Intent(this, YourMainClass.class); 
i.putExtra("key", value); 

其次:

String value = getIntent().getExtras().getString("key"); 

而且可以添加多个 “其他” 或类似这样的事情?

i.putExtra("key", value1); 
i.putExtra("key2", value2); 
i.putExtra("key3", value3); 

谢谢...

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); 
相关问题