Android:在类之间传递参数 [英] Android: passing parameters between classes
本文介绍了Android:在类之间传递参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个 class2,它在点击时由 class1 参与.我必须将一些参数/对象从 class1 传递到 class2.我只知道没有参数传递选项的标准方式.
I have a class2 which is involved by class1 when clicks are made. I have to pass some parameters/objects from class1 to class2. I only know the standard way which does not have an option of passing parameters.
// launch the full article
Intent i = new Intent(this, Class2.class);
startActivity(i);
推荐答案
您可以使用 Intent.putExtra
(使用 Bundle
)来传递额外的数据.>
You can use Intent.putExtra
(Which uses a Bundle
) to pass extra data.
Intent i = new Intent(this, Class2.class);
i.putExtra("foo", 5.0f);
i.putExtra("bar", "baz");
startActivity(i);
然后,一旦您进入新的Activity
:
Then once you're inside your new Activity
:
Bundle extras = getIntent().getExtras();
if(extras !=null)
{
float foo = extras.getFloat("foo");
String bar = extras.getString("bar");
}
这允许您将基本数据传递给活动.但是,您可能需要做更多的工作来传递任意对象.
This allows you to pass basic data to Activities. However, you may need a bit more work for passing arbitrary objects along.
这篇关于Android:在类之间传递参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文