Java 需要为Android游戏节省5次最佳时间

Java 需要为Android游戏节省5次最佳时间,java,android,Java,Android,我需要为一个游戏保存5个整数的最佳次数。这个游戏是一个反应时间游戏 我假设最简单的方法是把它存储在一个文本文件中,但我真的不知道该怎么做 protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); startButton = (Button) findViewById(R.i

我需要为一个游戏保存5个整数的最佳次数。这个游戏是一个反应时间游戏

我假设最简单的方法是把它存储在一个文本文件中,但我真的不知道该怎么做

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    startButton = (Button) findViewById(R.id.btnStart);
    textTimer = (TextView) findViewById(R.id.textTimer);
    myHandler = new Handler();

    startButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            startTime = SystemClock.uptimeMillis();
            myHandler.postDelayed(updateTimerMethod, 0);
            MainActivity.this.textTimer.setVisibility(0);
        }
    });

    pauseButton = (Button) findViewById(R.id.btnPause);

    pauseButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            timeSwap += timeInMillies;
            myHandler.removeCallbacks(updateTimerMethod);
            MainActivity.this.textTimer.setVisibility(0);
        }
    });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

private Runnable updateTimerMethod = new Runnable() {

    @Override
    public void run() {
        timeInMillies = SystemClock.uptimeMillis() - startTime;
        finalTime = timeSwap + timeInMillies;

        int seconds = (int) (finalTime / 1000);
        int minutes = seconds / 60;
        seconds = seconds % 60;
        int milliseconds = (int) (finalTime % 1000);
        textTimer.setText("" + minutes + ":" + String.format("%02d", seconds) + ":" + String.format("%03d", milliseconds));
        myHandler.postDelayed(this, 0);
    }
};

这就是我现在的代码。我想做的是,当我按下暂停按钮时,它将转到下一个屏幕并显示时间。还有一个按钮,比如说记录。如果按下,它将显示5个最佳时间。

您可以使用SharedReferences putInt和getInt方法保存int值,而不是使用文件保存分数

下面的示例可能是它的一个非常懒惰的实现

protected final static String PREFS_NAME = "PREF_GAME_SCORE";

public static int GetBestScore(Context context, int no) 
{
    SharedPreferences settings = context.getSharedPreferences(PREFS_NAME, 0);
    return settings.getInt("Score" + String.valueOf(no), 0); // # 0 is the default value if no data is found
}

public static void SetBestScore(Context context, int no, int score)
{
    SharedPreferences settings = context.getSharedPreferences(PREFS_NAME, 0);
    SharedPreferences.Editor editor = settings.edit();
    editor.putInt("Score" + String.valueOf(no), score);
    editor.commit();
}
您可以使用SetBestScoreconxtext,1666;要设置最佳分数编号:1,要获得最佳分数,请使用;getScoreContext,1

使用SharedReferences保存的值将保留,直到应用程序从设备中删除或使用以下方法手动清除应用程序数据:

设置->应用程序->管理应用程序->选择您的应用程序 ->清晰数据


有三种方法可以做到这一点 1.平面锉刀 2.共享首选项 3.SQL

就我个人而言,我建议您使用SQL,因为您希望在将来添加的任何操作都会减少工作量。如果使用SharedReference,您需要一组全新的代码来处理新操作。因此,最好使用sqlite

每当比赛结束时,记下分数并把它放在桌上。当您想要显示前5个结果时。按分数升序进行查询,最多可查询5行。宾果游戏你可以得到你想要的结果

希望能有帮助

看看这个链接,了解如何从android创建和使用SQL数据库。

在前面所说的基础上,这里有一种方法可以发送一个分数,并检查它是否在最高分和最低分之间。如果是,则设置临时点,然后相应地更新

你也可以设置任意数量的最高分。这可以很容易地修改,以获得另一种方式,即检查高分

public static void CheckAndSetBestScore(Context context, int score) {
    int TOTAL_TOP_SCORES = 5;
    int TEMP_SPOT = -1;

    SharedPreferences settings = context
            .getSharedPreferences(PREFS_NAME, 0);
    SharedPreferences.Editor editor = settings.edit();

    for (int i = 1; i <= TOTAL_TOP_SCORES; ++i) {

        if (TEMP_SPOT == -1) {
            if (score <= settings.getInt("Score" + String.valueOf(i), 1000)) {
                TEMP_SPOT = i;
            }
        }
    }
    if (TEMP_SPOT != -1) {
        for (int i = TOTAL_TOP_SCORES; i >= TEMP_SPOT; --i) {
            if (i != TEMP_SPOT) {
                editor.putInt("Score" + String.valueOf(i), settings.getInt("Score" + String.valueOf(i - 1), 1000));
            } 
            else {
                editor.putInt("Score" + String.valueOf(i), score);
            }
        }
    }
}

查看SharedReferences如Damien R所建议的,存储一些值的最简单方法是使用SharedReferences。您可以将它们存储为5个单独的整数值,这样就不会对字符串进行拆分、联接和转换,也不会使用单个字符串值将所有分数以…分隔。。。逗号或任何你喜欢的分隔符。读这个:我很确定它会给你你需要的一切。好了,现在问题变了,我相信你的问题得到了回答。如果你在问新问题之前还有其他问题,请确保你读了一些材料,如果已经有人问了,请搜索。在上次编辑之后,我意识到你2天前已经问过这个问题了。如果是两天,我可能会认为对到底发生了什么缺乏了解。我建议你试着逐行理解代码,在谷歌中搜索,阅读中的共享参考。因此,用户无法为您编写整个应用程序。应用程序的更新之间可能会保留重复的和值。我对此投了否决票。使用SharedReferences保存的值将保留,直到应用程序从设备中删除为止。只有在用户清除“应用程序设置”中的应用程序数据之前,它们才会消失。@Zefnus我怎样才能用您给出的代码记录5个最佳时间?我可以和你分享我的代码吗,这样你就可以修改它了?“我还是不明白。”Yogamurthy谢谢你的指点。我完全忘记了清除应用程序数据。但如果应用程序也被删除,数据将被删除。请检查我的编辑。@Al Rayyan尝试复制粘贴样本,并在onCreate中添加SetBestScorethis,1,22;设置最佳时间1,即:22。运行应用程序。关闭您的应用程序。删除刚才添加的行SetBestScore。再次使用onCreate add int testInt=GetBestScorethis,1;。看看你能不能得到22的值。当然,如果您认为编辑您的问题可能有助于解决问题,请共享您的代码;SQLite将是一个很好的方法,但我不认为只对5个int值使用它是真正必要的。处理数据库创建、表、行。平均值是多少,特别是在5行中排名前5位?