我需要保存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次最好的时间。
发布于 2014-03-05 15:11:12
您可以使用SharedPreferences 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();
}您可以使用SetBestScore(conxtext, 1, 666);设置最佳得分#编号:1,并获得最佳得分,使用;GetBestScore(context, 1);
使用SharedPreferences保存的值将保持不变,直到应用程序从设备中删除或使用以下方法手动清除应用程序数据为止:
设置->应用程序->管理应用程序-> (选择应用程序) ->清除数据
发布于 2014-03-05 16:45:00
在Zefnus所说的基础上,这里有一种方法可以发送一个分数,并检查它是否在得分最高的地方。如果是,则设置TEMP_SPOT,然后相应地进行更新。
你也可以设置任意数量的高分。这可以很容易地修改,以获得另一种方式,IE,检查高分。
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);
}
}
}
}发布于 2014-03-05 15:24:11
有三种方法可以实现它: 1.平面FIle 2.共享首选项3. SQL
就我个人而言,我建议您使用SQL,因为将来要添加的任何操作都会使较小的work.If在共享首选项的情况下,您需要一套全新的代码来更好地处理一个新的operation.so。
每当一场比赛结束时,拿出比分并把它插入桌子上。当你想显示前5名结果时。按升级换代顺序查询得分,并限制为5行。好的,你可以得到你想要的结果。
希望能帮上忙。
看看这个链接,了解如何从android创建和使用SQL数据库。http://chrisrisner.com/31-Days-of-Android--Day-24%E2%80%93Using-SQLite-Databases
https://stackoverflow.com/questions/22201494
复制相似问题