我的问题是我不知道如何将高分评分保存到我的highscore.class
我的游戏的场景或流程是这样的。
用户完成测验游戏后,会弹出finish.class并有一个按钮,如果你按下该按钮,它会自动将文本设置为我的highscore.class
示例是: finish.class 保存高分(按钮) - 用户点击
highscore.class 设置文本(textview) - 如果用户点击finish.class中的save highscore按钮,它将自动显示
高分 10/10(用户得分)
希望你得到我的解释:)。真的很感谢你对我的问题的回答。新手在这里:)你能告诉我这个
的样本或源代码只是为了简单起见。一个按钮,将文本设置为另一个.class
finish.class
public class finishextends Activity {
Button save highscore;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.timesup);
// TODO Auto-generated method stub
save highscore= (Button) findViewById(R.id.save_highscore);
save highscore.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
}
}
highscore.class只是我的后退按钮
公共类HighScore扩展了活动{
Button back;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.highscore);
back = (Button) findViewById(R.id.btn_backk);
back.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(),"Back",
Toast.LENGTH_SHORT).show();
Intent intent = new Intent(getApplicationContext(),MainMenu.class);
startActivity(intent);
}
});
// TODO Auto-generated method stub
}
}
答案 0 :(得分:0)
您可以使用共享偏好来保存高分,就像那时您可以通过意图传递当前分数:
关于saveHighScore onClick:
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(Finish.this);
if (score > preferences.getInt("score", 0)) {
preferences.edit().putInt("score", score).commit();
}
Intent showHighScore = new Intent(Finish.this, HighScoreActivity.class);
showHighScore.putExtra("current_score", score);
startActivity(showHIghScore);
然后onCreate of your HighScoreActivity
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
int currentScore = getIntent().getIntExtra("current_score", 0);
highScore = (TextView) findViewById(R.id.highscore);
highScore.setText(currentScore + " / " + preferences.getInt("score", 0));
答案 1 :(得分:0)
目前尚不清楚你真正想做什么。但我的建议就在这里。
完成游戏后,您只需将此用户的分数保存在SQLite数据库中,并将其用于任何想要显示的位置。
其次,根据您的代码。您可以将意图中的分数传递给其他活动。当该活动开始时,您可以从意图中获得该分数。
public class finishextends Activity {
Button highscore;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.timesup);
// TODO Auto-generated method stub
highscore= (Button) findViewById(R.id.save_highscore);
highscore.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Pass your score to other activity through Android's intent.
Intent intent = new Intent(getApplicationContext(),
HighScore.class);
//THis highestScore variable will actually hold the score you get in this activity.
intent.putExtra("score", highestScore);
startActivity(intent);
}
});
}
}
现在在HighScore.java活动类中获取此数据。
public class HighScore extends Activity {
Button back;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.highscore);
int highestScore = -1;
//Now use this score variable to set anywhere.
Bundle extras = getIntent().getExtras();
if (extras != null) {
highestScore = extras.getString("score");
}
back = (Button) findViewById(R.id.btn_backk);
back.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(),"Back",
Toast.LENGTH_SHORT).show();
}
});
// TODO Auto-generated method stub
}
}