【问题标题】:how to retrieve a Double value from one activity to another?如何将 Double 值从一个活动检索到另一个活动?
【发布时间】:2013-02-01 09:19:53
【问题描述】:

我制作了一个包含 2 个活动的应用程序,其中第一个活动包含一些 EditText(十进制数字),另一个 ractivity 还包含一些 Edtitexts(十进制),现在我想将一个 EditText 的值传递给另一个,但作为“双" 不是字符串。因为这些值将用于数学计算。我希望严格地“双”值。 我试过如下:但不知道如何将字符串转换为“double”。

activity1.java

    final Double d1;
        final Double d2;
        final Double d3;
        final Double d4;
        final Double d5;
        final Double d6;
        final Double d7;
        final Double d8;
        d1=Double.parseDouble(et1.getText().toString());
        d2=Double.parseDouble(et2.getText().toString());
        d3=Double.parseDouble(et3.getText().toString());
        d4=Double.parseDouble(et4.getText().toString());
        d5=Double.parseDouble(et5.getText().toString());
        d6=Double.parseDouble(et6.getText().toString());
        d7=Double.parseDouble(et7.getText().toString());
        d8=Double.parseDouble(et8.getText().toString());
.
.
.
.
Intent ic = new Intent(Calculator_1Activity.this,Calculator2a.class);
        ic.putExtra("doubleValue_e1", d1);
        ic.putExtra("doubleValue_e2", d2);
        ic.putExtra("doubleValue_e3", d3);
        ic.putExtra("doubleValue_e4", d4);
        ic.putExtra("doubleValue_e5", d5);
        ic.putExtra("doubleValue_e6", d6);
        ic.putExtra("doubleValue_e7", d7);
        ic.putExtra("doubleValue_e8", d8);

        startActivity(ic);

Activity2.java

Intent receiveIntent = this.getIntent();
        e1 = receiveIntent.getDoubleExtra("doubleValue_e1", 0.00);
        e2 = receiveIntent.getDoubleExtra("doubleValue_e2", 0.00);
        e3 = receiveIntent.getDoubleExtra("doubleValue_e3", 0.00);
        e4 = receiveIntent.getDoubleExtra("doubleValue_e4", 0.00);
        e5 = receiveIntent.getDoubleExtra("doubleValue_e5", 0.00);
        e6 = receiveIntent.getDoubleExtra("doubleValue_e6", 0.00);
        e7 = receiveIntent.getDoubleExtra("doubleValue_e7", 0.00);
        e8 = receiveIntent.getDoubleExtra("doubleValue_e8", 0.00);

        Calculator_1Activity cal1 = new Calculator_1Activity();
        et1.setText(cal1.et1.getText().toString());

我已经尝试过了,但仍然无法正常工作。我的项目意外停止..

【问题讨论】:

标签: android android-intent casting double


【解决方案1】:

从第一个活动发送双精度:

Intent sendingIntent = new Intent(this,SecondActivity.class);
intent.putExtra("doubleValue_e1", doubleData);
intent.putExtra("doubleValue_e2", doubleData);
intent.putExtra("doubleValue_e3", doubleData);
intent.putExtra("doubleValue_e4", doubleData);
startActivity(sendingIntent);

在第二个活动中获得双倍:

double e1,e2,e3,e4;
Intent receiveIntent = this.getIntent();
e1 = intent.getDoubleExtra("doubleValue_e1", defaultValue)
e2 = intent.getDoubleExtra("doubleValue_e2", defaultValue)
e3 = intent.getDoubleExtra("doubleValue_e3", defaultValue)
e4 = intent.getDoubleExtra("doubleValue_e4", defaultValue)

【讨论】:

  • 我的朋友谢谢你,但你能告诉我如何将多个值从一个活动发送到另一个活动吗?
  • 有意添加更多额外内容。您可以通过一个意图发送多个值
  • 兄弟-我已按照您的建议编辑了我的代码,但我的朋友没有工作..我也编辑了我的问题。请查看..如果你能提供帮助..谢谢兄弟。
  • String in 转Double的原因?
【解决方案2】:
【解决方案3】:

Double.parseDouble(String s) 使用 Bundle 将信息从一个活动传递到另一个活动。

【讨论】:

    【解决方案4】:

    将你的 Double 解析为字符串,放入包中,然后在另一个活动中解析回字符串

    【讨论】:

      【解决方案5】:
      1. 使用Double.toString() 方法将双精度DATA 转换为字符串。
      2. 通过意图发送此字符串。

        intent.putExtra("DoubleWhichNowIsAString", DATA);
        
      3. 在接收器处取回

        getIntent().getStringExtra("DoubleWhichNowIsAString")
        
      4. 使用Double.parseDouble() 函数将字符串解析为双精度。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-08-17
        • 2011-10-27
        相关资源
        最近更新 更多