如何共享类之间的变量? [英] How do I share variables between classes?

查看:136
本文介绍了如何共享类之间的变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我做的东西像一个测验,我有一个计数器来显示已被正确回答问题的数量。当一个问题回答正确,以及一个新的屏幕(活动)所示,我怎么结转数到下一个屏幕?

Say I am making something like a quiz, and I have a counter to show the number of questions that have been answered correctly. When one question is correctly answered, and a new screen(Activity) is shown, how do I carry over the number to the next screen?

推荐答案

当你说屏幕做你的意思是活动?那么你可能想通过你的意图演员通过他们。

When you say screens do you mean Activities? Then you probably want to pass them via extras in your intents.

活动1:

    int score;

    ...
    Intent Intent = new Intent(...);
    intent.putExtra("score_key", score);
    startActivity(intent);

活动2的的onCreate()

    int score;
    ...

    Bundle extras = getIntent().getExtras();

    // Read the extras data if it's available.
    if (extras != null)
    {
        score = extras.getInt("score_key");
    }

这篇关于如何共享类之间的变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆