如何从一个活动到另一个活动中检索Double值? [英] how to retrieve a Double value from one activity to another?
问题描述
我制作了一个包含2个活动的应用程序,其中第一个活动包含一些EditText(十进制数字),另外,ractivity也包含一些Edtitexts(十进制),现在我想将一个EditText的值传递给另一个,但是作为"double ",而不是字符串.因为值将在数学计算中使用.我希望该值严格为"double". 我尝试过如下操作:但不知道如何将字符串转换为双精度".
I have made an application containing 2 activities ,in that first activity contains some EditTexts (decimal numbers),and anothe ractivity also contains some Edtitexts(decimal) ,now I want to pass one EditText's value to another but as a "double" not as a string.Because that values will be used in mathematical calculation. I want that values in "double" strictly. I have tried as below: but have no idea how to cast a String to "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());
我已经尝试过了,但是仍然无法正常工作.我的项目意外停止了..
i have tried this but still not working.my project stops unexpectly..
推荐答案
从第一个活动发送双打:
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)
这篇关于如何从一个活动到另一个活动中检索Double值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!