安卓:类之间传递参数 [英] Android: passing parameters between classes

查看:567
本文介绍了安卓:类之间传递参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类2 这是涉及由 1级点击后制成。我要通过一些参数/从 1级对象为类2 。我只知道它不具有传递参数选项的标准方法。

  //启动全文
意图I =新的意图(这一点,Class2.class);

startActivity(ⅰ);
 

解决方案

您可以使用 Intent.putExtra (它使用捆绑)来传递额外的数据。

 意向书我=新的意图(这一点,Class2.class);
i.putExtra(富,5.0F);
i.putExtra(酒吧,巴兹);
startActivity(ⅰ);
 

然后,一旦你是新的活动里面

 捆绑额外= getIntent()getExtras()。
如果(临时演员!= NULL)
{
 浮富= extras.getFloat(富);
 串吧= extras.getString(巴);
}
 

这可以让您将基础数据的活动。但是,您可能需要沿着传递任意对象多一点的工作。

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);

解决方案

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);

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.

这篇关于安卓:类之间传递参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆