我是android新手,所以为了学习,我给自己做了一个简单的游戏,用户在一个活动“ scene1 ”中找到一个图像,然后一旦他们按下图像,它就会移动到下一个活动“ scene2 ”,一切正常。我想要做的是保存用户达到的级别,以便他们下次启动游戏时可以通过单击加载保存数据按钮继续。
我已经查看了共享首选项并写入内部存储中的文件,但我似乎无法理解它:(如果你能给我一些帮助并让我朝着正确的方向前进,那将非常有帮助:)
下面是我用于每个级别的代码。
public class scene1 extends Activity implements OnClickListener {
private CountDownTimer countDownTimer;
private boolean timerHasStarted = false;
private Button startB;
public TextView text;
private final long startTime = 20 * 1000;
private final long interval = 1 * 1000;
@Override
public void onBackPressed() {
new AlertDialog.Builder(this)
.setIcon(android.R.drawable.ic_dialog_alert)
.setTitle("FindIt")
.setMessage("Exit to main menu?")
.setPositiveButton("Yes", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
})
.setNegativeButton("No", null)
.show();
}
ImageButton imageButton;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
startB = (Button) this.findViewById(R.id.button);
startB.setOnClickListener(this);
text = (TextView) this.findViewById(R.id.timer);
countDownTimer = new MyCountDownTimer(startTime, interval);
text.setText(text.getText() + String.valueOf(startTime/1000));
}
@Override
public void onClick(View v) {
startB.setVisibility(View.INVISIBLE);
if (!timerHasStarted) {
countDownTimer.start();
timerHasStarted = true;
startB.setText("STOP");
} else {
countDownTimer.cancel();
timerHasStarted = false;
startB.setText("RESTART");
}
}
public class MyCountDownTimer extends CountDownTimer {
public MyCountDownTimer(long startTime, long interval) {
super(startTime, interval);
}
@Override
public void onFinish() {
Intent intent = new Intent(scene1.this, timeUp.class);
scene1.this.startActivity(intent);
finish();
}
@Override
public void onTick(long millisUntilFinished) {
text.setText("" + millisUntilFinished/1000);
addListenerOnButton();
}
public void addListenerOnButton() {
imageButton = (ImageButton) findViewById(R.id.it);
imageButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Toast.makeText(scene1.this,
"You found It!", Toast.LENGTH_LONG).show();
Intent intent = new Intent(scene1.this, scene2.class);
scene1.this.startActivity(intent);
android.os.Process.killProcess(android.os.Process.myPid());
finish();
}
});
}
}
}