1

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

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

在此之后我应该怎么做才能让我的变量进入 Power 类..

4

3 回答 3

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);
于 2013-09-07T08:49:22.053 回答
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.
于 2013-09-07T08:49:35.517 回答
0

试试这个::

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

在您的 Power 活动中;

     double payback= getIntent().getDoubleExtra (String key, double defaultValue);
于 2013-09-07T08:54:44.210 回答