我想通过在其他活动中一起计算3个数字来使用从代码中获得的变量(答案),我想使用从活动2中对活动1进行的先前计算得出的答案来计算新变量。我尝试在Internet上搜索,但是内容并不多,或我只是想将文本值传递到另一个页面并替换它。我以某种方式设法在第一时间就实现了这个想法,但我的模拟器无法正常工作。
这是我的代码,希望我能做到。
活动1
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_male);
age =(TextView)findViewById(R.id.age);
height=(TextView)findViewById(R.id.heigth);
weight =(TextView)findViewById(R.id.weigth);
result =(TextView)findViewById(R.id.result);
eage=(EditText)findViewById(R.id.eage);
eheight=(EditText)findViewById(R.id.eheight);
eweight=(EditText)findViewById(R.id.eweight);
calculate=(Button)findViewById(R.id.calculate);
back =(Button)findViewById(R.id.back);
next =(Button)findViewById(R.id.next);
calculate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
double n1=Double.parseDouble(eage.getText().toString());
double n2=Double.parseDouble(eheight.getText().toString());
double n3=Double.parseDouble(eweight.getText().toString());
double ans=(90+4.8*n2+13.4*n3-5.7*n1);
result.setText("Your BMR is "+ans);
}
});
back.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i=new Intent(MALE.this,MainActivity.class);
startActivity(i);
}
});
next.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent(MALE.this, MALERUN.class);
double n1=Double.parseDouble(eage.getText().toString());
double n2=Double.parseDouble(eheight.getText().toString());
double n3=Double.parseDouble(eweight.getText().toString());
double ans=(90+4.8*n2+13.4*n3-5.7*n1);
result.setText("Your BMR is "+ans);
i.putExtra("answer",ans);
startActivity(i);
}
});
}
活动2
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_malerun);
calculate=(Button)findViewById(R.id.calculate);
back =(Button)findViewById(R.id.back);
next =(Button)findViewById(R.id.next);
result =(TextView)findViewById(R.id.result);
time=(EditText)findViewById(R.id.time);
calculate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (time.getText().equals(0));
Intent i = getIntent();
double[] n1=i.getDoubleArrayExtra("answer");
double ans = (n1*1.2);
}
});
}
答案 0 :(得分:2)
您可以使用Intent
Intent intent = new Intent(Activity1.this, Activity2.class);
intent.putExtra("ANSWER", answer);
startActivity(intent);
在Activity 2.class上创建
double answer = getintent().getDoubleExtra("ANSWER", 0.0);
答案 1 :(得分:1)
最佳方式:
活动1:
public static String myVar;
活动2:
String m=activity1.myVar;