我试图通过调用布尔值来检查我的游戏中的关卡是否完整。我想在我的活动中按下一个按钮,然后检查关卡是否完成,如果“真”,关卡将加载,如果“假”,它将显示一个吐司说关卡已锁定。我可以将布尔值正确地保存在我的首选项中,但将其回调正变得非常痛苦。下面是我的代码。
我的保存方法:
SharedPreferences gamesettings =
this.getSharedPreferences("GameProgress",0);
SharedPreferences.Editor Edit = gamesettings.edit();
Edit.putBoolean("level1",true);
Edit.commit();
My menu where i want to call the boolean and start the levels activity:
public class DebugMain extends Activity {
private Button button;
private Button button1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.debugmenu);
addListenerOnButton();
}
public void addListenerOnButton() {
button = (Button) findViewById(R.id.button1);
button1 = (Button) findViewById (R.id.button2);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
loadSavedPreferences(); {
SharedPreferences sharedPreferences = PreferenceManager
.getDefaultSharedPreferences(this);
boolean gamesettings = sharedPreferences.getBoolean("level1", true);
if (gamesettings) {
levelcomplete.setChecked(true);
Intent intent = new Intent(DebugMain.this, scene1.class);
DebugMain.this.startActivity(intent);
finish();
} else {
levelcomplete.setChecked(false);
Toast.makeText (getApplicationContext(), "Level Locked", Toast.LENGTH_LONG).show ();
}
}
});
button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
//is level 2 unlocked going here
}
});
}
}