将多个变量值发送到另一个活动
这是我用来将字符串变量的值传输到另一个活动的代码。
Intent requestLink = new Intent(Search.this, Results.class);
requestLink.putExtra("Link", sendLink);
startActivity(requestLink);
但是如果我想传输多个变量怎么办?
Intent requestLink = new Intent(Search.this, Results.class);
requestLink.putExtra("Link", sendLink);
startActivity(requestLink);
Intent userSearch = new Intent(Search.this, Results.class);
userSearch.putExtra("Search", addressInput);
startActivity(userSearch);
使用代码两次将像上面一样只会启动两个单独的活动。那么,如何同时传输这些值呢?
我对 Android 开发和 OOP 还很陌生。
So this is the code that I use to transfer the value of a string variable to another activity.
Intent requestLink = new Intent(Search.this, Results.class);
requestLink.putExtra("Link", sendLink);
startActivity(requestLink);
But what if I wish to transfer more than one variable.
Intent requestLink = new Intent(Search.this, Results.class);
requestLink.putExtra("Link", sendLink);
startActivity(requestLink);
Intent userSearch = new Intent(Search.this, Results.class);
userSearch.putExtra("Search", addressInput);
startActivity(userSearch);
Using the code twice will like the above will only just start two separate activities. So, how can I transfer the values simultaneously?
I'm still pretty new to Android development and also OOP.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以添加多次调用 putExtra 来实现相同的意图:
You can add more than call putExtra more than once for the same intent:
您可以使用 Bundle 在 Activity 之间发送数据。
例如,
查看 Bundle api 文档此处
You can use Bundle for sending data between your Activities.
e.g
Check Bundle api documentation here
只需将两个字符串放在相同的意图中即可。
Just put both Strings in the same intent.
根据需要,bundle.get... 有许多重载,例如 getInt,...。
bundle.get... has many overloads like getInt,... depending on the need.