我是Java新手并编写Android游戏应用程序,我刚学会编写一个简单的射击游戏应用程序。现在我想保存我的高分,我发现有一个所谓的
sharedpreferences
可用于存储分数。但我真的不明白如何使用它。任何人都可以帮助我吗?
以下是我的游戏类
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//turn title off
requestWindowFeature(Window.FEATURE_NO_TITLE);
//set to full screen
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(new GamePanel(this));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_game, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
我在 GamePanel.java 课程中对所有游戏活动进行了编码,在这个课程中我也计算了我的分数。所以现在我想存储分数。我该怎么办?
答案 0 :(得分:0)
SharedPreferences spref = getSharedPreferences("your_prefs_name", Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = spref.edit();
editor.putString("deficitPercentage_key", prefVal); //
editor.commit();
prefVal是要存储的值