我是安卓初学者。我有四个阶段(四个 Activity )。在 Activity 一中,当您单击按钮时,您必须存储值。例如 counter1=3
。每个阶段都像第一阶段。我想知道您能否告诉我如何将这些整数值加在一起并显示出来。
我要加:counter1+counter2+counter3+counter4=score
如何将这些阶段连接在一起?
最佳答案
传递您对使用 intent extra 的信任。下面是一个如何保持递增计数的示例,但您可以从每个 Activity 中传递它们并在最后将它们相加。
Activity 1
int count = 5;
导航 Activity 1 - Activity 2
Intent intent = new Intent(this, Activity2.class);
intent.putExtra("counter", count);
Activity 2
int count = getIntent().getIntExtra("counter", 0);
count += 3; // count is 8
导航 Activity2 - Activity 3
Intent intent = new Intent(this, Activity3.class);
intent.putExtra("counter", count);
Activity 3
int count = getIntent().getIntExtra("counter", 0);
count += 2; // count is 10
导航 Activity 3 - Activity 4
Intent intent = new Intent(this, Activity4.class);
intent.putExtra("counter", count);
Activity 4
int count = getIntent().getIntExtra("counter", 0);
count += 1;
Log.i("TAG", "Your count is:"+count); // 11
关于android - 在Android应用程序的不同 Activity 中添加四个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9285357/