在Android应用程序开发中的两个活动之间传输变量

时间:2013-09-07 08:43:33

标签: android android-intent

我正在尝试将我的变量从一个活动页面转移到另一个活动页面,但在某个点之后无法向前移动(Android应用程序开发的新功能),这是我的代码的一部分:

    double payback = num/int;
    double money = v*i*t*e/1000;
    startActivity(new Intent(MainActivity.this, Power.class));

我应该怎么做才能将我的变量变成类Power ..

3 个答案:

答案 0 :(得分:0)

尝试这样

Intent intent = new Intent(MainActivity.this , Power.class);
intent.putExtra(String key , double value);
startActivity(intent);

在电源活动中,您可以通过

检索值
Intent intent = getIntent();
double payback = intent.getDoubleExtra (String key, double defaultValue);

答案 1 :(得分:0)

使用意图

  Intent i = new Intent(MainActivity.this, Power.class)
  i.putExtra("key",money);
  startActivity(i); 

要检索

 Bundle extras = getIntent().getExtras();
 if(extras!=null)
 {
           double value = extras.getDoubleExtra ("key",defaultvalue);
 }

http://developer.android.com/reference/android/content/Intent.html

putExtra

public Intent putExtra (String name, double value)

Added in API level 1
Add extended data to the intent. The name must include a package prefix, for example the app com.android.contacts would use names like "com.android.contacts.ShowAll".

Parameters
name    The name of the extra data, with package prefix.
value   The double data value.
Returns
Returns the same Intent object, for chaining multiple calls into a single statement.

getDoubleExtra

public double getDoubleExtra (String name, double defaultValue)

Added in API level 1
Retrieve extended data from the intent.

Parameters
name    The name of the desired item.
defaultValue    the value to be returned if no value of the desired type is stored with the given name.
Returns
the value of an item that previously added with putExtra() or the default value if none was found.

答案 2 :(得分:0)

试试这个::

Intent intent = new Intent(MainActivity.this, Power.class);
intent.putExtra("payback",payback );
intent.putExtra("money",money);
startActivity(intent );\

在您的电力活动中;

     double payback= getIntent().getDoubleExtra (String key, double defaultValue);