我有一个有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);
}
});
}
}
highscore.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
}
}
答案 0 :(得分:0)
int currentScore = getIntent().getIntExtra("current_score", 0)
您在这里得到0,因为如果您的意图有误,默认值为0。
请务必先设置得分
showHighScore.putExtra("current_score", score);
您只需更改
int currentScore = getIntent().getIntExtra("current_score", 0);
到
int currentScore = getIntent().getIntExtra("current_score", 999);
以查看999/3
,以便您知道出了什么问题