android - 在Android应用程序的不同 Activity 中添加四个值

标签 android

我是安卓初学者。我有四个阶段(四个 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/

相关文章:

android webview 占满整个屏幕

android - 使用 Achartengine 动态更新图表

android - 手动启动/停止接收器并将参数传递给接收器

android - 如何连续获取正在播放的歌曲的音量?

java - Android 工作室崩溃了。任何项目都行不通,有红线

android - 从 ViewPager 中的 Activity 更新 fragment

android - 是否可以通过 ADB 通过 ID 单击 Android 上的按钮,而不仅仅是通过键码点击 x、y 位置或键?

android - 在android中显示二维按钮数组

javascript - 仅 Android 4.0 设备上的 Phonegap getCurrentPosition 超时

android - 如何使用 WHERE 子句查询我的数据库并使用 ORDER BY DESC 获取最近 5 条记录