我的问题是,如何使用文本文件创建保存文件。我希望能够保存游戏并在手机重启后加载。
所以我要问的是如何使用文本文件制作保存文件。
public class MainActivity extends AppCompatActivity {
ImageButton Button,Button2,Button3,Button4,Button5,Button6,Button7,Button8,Button9,Button10;
long count = 0;
MediaPlayer mp, mp_pilla,mp_slua;
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView textview = (TextView) findViewById(R.id.textView);
mp = MediaPlayer.create(MainActivity.this, R.raw.nyaaw);
mp_pilla = MediaPlayer.create(MainActivity.this, R.raw.pillabli);
mp_slua = MediaPlayer.create(MainActivity.this, R.raw.slua);
Button = (ImageButton) findViewById(R.id.myButton);
Button2 = (ImageButton) findViewById(R.id.myButton2);
Button3 = (ImageButton) findViewById(R.id.myButton3);
Button4 = (ImageButton) findViewById(R.id.myButton4);
Button5 = (ImageButton) findViewById(R.id.myButton5);
Button6 = (ImageButton) findViewById(R.id.myButton6);
Button7 = (ImageButton) findViewById(R.id.myButton7);
Button8 = (ImageButton) findViewById(R.id.myButton8);
Button9 = (ImageButton) findViewById(R.id.myButton9);
Button10 = (ImageButton) findViewById(R.id.myButton10);
textview.setText("0");
ImageButton buttonsak = (ImageButton) findViewById(R.id.myButton);
ImageButton buttonsak2 = (ImageButton) findViewById(R.id.myButton2);
ImageButton buttonsak3 = (ImageButton) findViewById(R.id.myButton3);
ImageButton buttonsak4 = (ImageButton) findViewById(R.id.myButton4);
ImageButton buttonsak5 = (ImageButton) findViewById(R.id.myButton5);
ImageButton buttonsak6 = (ImageButton) findViewById(R.id.myButton6);
ImageButton buttonsak7 = (ImageButton) findViewById(R.id.myButton7);
ImageButton buttonsak8 = (ImageButton) findViewById(R.id.myButton8);
ImageButton buttonsak9 = (ImageButton) findViewById(R.id.myButton9);
ImageButton buttonsak10 = (ImageButton) findViewById(R.id.myButton10);
View.OnClickListener clicker = new View.OnClickListener() {
public void onClick(View v) {
switch (v.getId()) {
case R.id.myButton:
count++;
mp.start();
textview.setText("" + count);
break;
case R.id.myButton2:
if (count >= 50) {
count += 5;
mp_slua.start();
textview.setText("" + count);
}
break;
case R.id.myButton3:
if (count >= 100) {
count += 10;
mp_pilla.start();
textview.setText("" + count);
}
break;
case R.id.myButton4:
if (count >= 500) {
count += 20;
mp_slua.start();
textview.setText("" + count);
}
break;
case R.id.myButton5:
if (count >= 2000) {
count += 50;
mp_slua.start();
textview.setText("" + count);
}
break;
case R.id.myButton6:
if (count >= 5000) {
count += 100;
mp.start();
textview.setText("" + count);
}
break;
case R.id.myButton7:
if (count >= 10000) {
count += 200;
mp_slua.start();
textview.setText("" + count);
}
break;
case R.id.myButton8:
if (count >= 30000) {
count += 500;
mp_pilla.start();
textview.setText("" + count);
}
break;
case R.id.myButton9:
if (count >= 100000) {
count += 1000;
mp.start();
textview.setText("" + count);
}
break;
case R.id.myButton10:
if (count >= 250000) {
count += 2500;
mp_pilla.start();
textview.setText("" + count);
}
break;
default:
break;
}
}
};
buttonsak.setOnClickListener(clicker);
buttonsak2.setOnClickListener(clicker);
buttonsak3.setOnClickListener(clicker);
buttonsak4.setOnClickListener(clicker);
buttonsak5.setOnClickListener(clicker);
buttonsak6.setOnClickListener(clicker);
buttonsak7.setOnClickListener(clicker);
buttonsak8.setOnClickListener(clicker);
buttonsak9.setOnClickListener(clicker);
buttonsak10.setOnClickListener(clicker);
}
答案 0 :(得分:3)
使用SharePreference保存这样的数据:
SharedPreferences.Editor editor = getSharedPreferences(SAVE_GAME, MODE_PRIVATE).edit();
editor.putString("game", "saved");
editor.commit();
并获取如下信息:
SharedPreferences prefs = getSharedPreferences(SAVE_GAME, MODE_PRIVATE);
String restoredText = prefs.getString("game", null);
if (restoredText != null) {
String name = prefs.getString("game", "state");// "state" is the default value.
}
答案 1 :(得分:1)
正如@Mariano所说:SharedPreferences是一个拯救事物的好地方。 SharedPreferences的优点是它不需要任何额外的权限。
根据您的需要,还有其他选择。您可以写入外部存储(需要WRITE_EXTERNAL)权限,也许更有趣的是使用service that Google provides来保存游戏状态。你可以在这里找到一些很好的例子。