我有一个问答游戏,它有 3 个问题/关卡问答游戏,但是如果我点击我的 mainactivity.class 中的按钮,我怎么能将我的高分文本设置为 2/3 或 1/3 或 3/3。我的代码流程是这样的:我的 mainactivity 上有一个高分按钮,如果你点击它,你会在 highscore.class 中设置分数。
public class MainActivity extends Activity {
Button highscore;
String highestScore;
int score;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// TODO Auto-generated method stub
highscore = (Button) findViewById(R.id.save);
highscore.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(MainActivity.this);
if (score > preferences.getInt("score", 0)) {
preferences.edit().putInt("score", score).commit();
}
Intent showHighScore = new Intent(MainActivity.this, highscore.class);
showHighScore.putExtra("current_score", score);
startActivity(showHighScore);
}
});
}
}
高分.class
public class highscore extends Activity {
Button back;
TextView highScore;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.highscore);
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
int currentScore = getIntent().getIntExtra("current_score", 0);
highScore = (TextView) findViewById(R.id.tv_highscore);
highScore.setText(currentScore + " / " + preferences.getInt("score", 3));
back = (Button) findViewById(R.id.btn_back);
back.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(),"Back",
Toast.LENGTH_SHORT).show();
}
});
// TODO Auto-generated method stub
}
}