Android Studio:如何通过sharedPreferences创建一个最佳分数?

时间:2014-08-15 13:09:01

标签: android button sharedpreferences

创建高分视图时遇到问题。游戏的目的是在15秒内在按钮上单击多次,我想在右角出现一个高分。这是我的代码:

public class newgame extends Activity {
    int clicks = 0;
    TextView textCount;
    Button buttonCount;
    int guessCount =0;

    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_newgame);
    final int oldscore = getSharedPreferences("myPrefs", MODE_PRIVATE).getInt("highscore", 0);

    final TextView textView = (TextView) findViewById(R.id.applesEaten);
    buttonCount = (Button) findViewById(R.id.button);

    buttonCount.setOnClickListener(new View.OnClickListener() {
        public void onClick(View arg0) {
            clicks++;
            textView.setText("Clicks: " + clicks);
            TextView textView = (TextView)findViewById(R.id.topScoreView);
                    textView.setText("Best: " + oldscore);
        }
    });
    final TextView textic = (TextView) findViewById(R.id.textView2);
    CountDownTimer Count = new CountDownTimer(15000, 1000) {
        public void onTick(long millisUntilFinished) {
            int seconds = (int) ((millisUntilFinished / 1000));
            textic.setText("Time Left: " + millisUntilFinished / 1000);
        }

        public void onFinish() {
            textic.setText("Time's Up!");
            buttonCount.setEnabled(false);
            if (clicks > oldscore)
                getSharedPreferences("myPrefs", MODE_PRIVATE).edit().putInt("highscore", clicks).commit();
        }
    };
    Count.start();
}

我将如何继续实现这一目标?

3 个答案:

答案 0 :(得分:0)

Zhuinden的评论几乎就是你想要的。但是我会尝试提出一些可能适合的东西 - 虽然我建议你自己尝试一下这些东西

添加以下变量:

int topScore = 0;
SharedPreferences prefs;

将此添加到您的onCreate方法

prefs = _context.getSharedPreferences(
            "com.example.app", Context.MODE_PRIVATE);

TextView topScore = (TextView) findViewById(R.id.topScoreView);

topScore.setText(prefs.getInt("topScore", 0);

将您的onFinish更改为以下内容:

    public void onFinish() {
        textic.setText("Time's Up!");
        buttonCount.setEnabled(false);
        if (guessCount > topScore) {
            prefs.edit().putInt("topScore", guessCount).apply();
        }
    }

我认为这应该有效,但实际上还没有测试过它

答案 1 :(得分:0)

为sharedPreferences添加值

getSharedPreferences("myPrefs", MODE_PRIVATE).putInt("highscore", clicks).commit();

检索值frome sharedPreferences

getSharedPreferences("myPrefs", MODE_PRIVATE).getInt("highscore", 0);

实现:

public class newgame extends Activity {
    int clicks = 0;
    TextView textCount;
    Button buttonCount;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_newgame);
        final int oldscore = getSharedPreferences("myPrefs", MODE_PRIVATE).getInt("highscore", 0);

        final TextView textView = (TextView) findViewById(R.id.textView);
        buttonCount = (Button) findViewById(R.id.button);

        buttonCount.setOnClickListener(new View.OnClickListener() {
            public void onClick(View arg0) {
                clicks++;
                textView.setText("Clicks: " + clicks + "(old highscore = " + oldscore + ")");
            }
        });
        final TextView textic = (TextView) findViewById(R.id.textView2);
        CountDownTimer Count = new CountDownTimer(15000, 1000) {
            public void onTick(long millisUntilFinished) {
                int seconds = (int) ((millisUntilFinished / 1000));
                textic.setText("Time Left: " + millisUntilFinished / 1000);
            }

            public void onFinish() {
                textic.setText("Time's Up!");
                buttonCount.setEnabled(false);
                if (clicks > oldscore)
                    getSharedPreferences("myPrefs", MODE_PRIVATE).edit().putInt("highscore", clicks).commit();
            }
        };
        Count.start();
    }
}

您的代码可以通过一些调整来完成

答案 2 :(得分:0)

好的,为了使用共享首选项存储您存储在guessCount变量中的分数值,您首先需要获取应用程序的共享首选项。为此,您可以使用:

SharedPreferences sharedPrefs = this.getSharedPreferences(
"com.example.yourAppName", Context.MODE_PRIVATE);

完成此操作后,您可以使用以下方法将分数值添加到首选项中:

sharedPrefs.putInt("High Score", guessCount).apply();

然后,当你想要检索高分以供使用时,你需要确保你仍然按照我上面描述的方式拥有一个SharedPreferences对象,但是要从中读取数据你只需使用:

int retrieveCount = sharedPrefs.getInt("High Score", 0)

retrieveCount中的值将是您之前存储的任何值。请注意确保SharedPreferences中存储了值,否则您将返回0。

此外: Android Documentation for SharedPreference