2015-01-06 107 views
-4

我正在创建一个名为电子衣柜的应用程序,用户将填写一些输入信息,如“布类型”,“季节”,“颜色”等。例如,当用户将冬天写入季节字段时,该应用会将类型和颜色字符串传递到WinterActivity中,但我不知道如何执行此操作。我希望你会帮助我,thanx提前:)Android:在活动间传递数据

+3

你有没有在谷歌搜索标题?你会发现许多很多示例代码和教程。 –

+2

你甚至打扰Google搜索吗?搜索'intent.putExtra'开始。 –

+0

是的,我做了,我尝试了一些东西,但他们没有工作。 :( – user3676303

回答

0

你可以检查季节字段,然后确定什么活动从该字段开始,然后将你想传递的其他信息与它一起作为一个捆绑意图。或者更好的做法是不做一个冬季活动只是做一个SeasonActivity,只是传递所有其他信息,然后让活动决定它需要什么样子以及它应该与用户分享哪些数据。

从A传送的数据 - > B在意图

而是从A - > B(对话框或第二步骤) - >经由ActivityForResult

+0

最好让它OP在给出整个解决方案之前展示了一些努力。 –

+1

没有给出任何代码帮助:)只是理论。如果他们有他们编写并需要帮助的代码,我将永远不会帮助他们 –

0

做你需要束数据添加到这样

//create the intent and bundle 
Intent launchIntent = new Intent(
MenuActivity.this, com.you.AnotherActivity.class); 
Bundle extraData = new Bundle(); 

//assign some values to the bundle 
extraData.putSerializable("key1", "value"); 

//assign bundle to launchIntent 
launchIntent.putExtra("keyBundle", extraData); 

//start the activity 
startActivity(launchIntent); 

推出的意图,然后检索中的其他活动的数据是这样的:

//create a new intent to put the one that started this activity 
Intent intent = getIntent(); 

//retrieve the bundle that was sent in the intent 
Bundle recievedBundle = 
     intent.getBundleExtra("keyBundle"); 


//get the bundle passed from the activity that started this one 
MyVariableInAnotherActivity = recievedBundle.getSerializable("key1"); 
0

试试这个:

Intent intent = new Intent(CurrentActivity.this, WinterActivity.class); 
intent.putExtra("cloth", clothTextField.getText().toString()); 
intent.putExtra("color", colorTextField.getText().toString()); 
startActivity(intent); 

在冬季活动收到他们喜欢:

String cloth = getIntent().getStringExtra("cloth"); 
String color = getIntent().getStringExtra("color"); 

希望它可以帮助!

相关问题