我对按钮有疑问。在基于达到玩级别1的分数后成功解锁级别2.我继续测试级别2并确保它可以基于分数要求解锁级别3,但是,在成功解锁级别3之后。级别2再次变为锁定。我真的不确定出了什么问题。我可能有我的逻辑错误或其他东西。在将Level 2按钮设置为启用true后,它应该已经保持启用(true)。我将展示我的代码相关。知道答案的人请指导我。
这是在LevelPage活动中,它包含所有级别按钮,供用户选择要播放的级别,因为此活动中的代码太长,我将只显示相关内容。
DBHelper db = new DBHelper(LevelPage.this);
HashMap<String,String> data = db.getData();
final String level1Score = data.get("LEVEL1");
final String level2Score = data.get("LEVEL2");
final String level3Score = data.get("LEVEL3");
final String level4Score = data.get("LEVEL4");
btnLevel1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
clickSound.start();
Intent intent = new Intent(LevelPage.this, Level1.class);
startActivity(intent);
}
});
if (level1Score != null && level1Score.equals("30")) {
btnLevel2.setEnabled(true);
btnLevel2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
clickSound.start();
Intent intent = new Intent(LevelPage.this, Level2.class);
startActivity(intent);
}
});
} else {
btnLevel2.setEnabled(false);
}
if (level2Score != null && level2Score.equals("35")) {
btnLevel3.setEnabled(true);
btnLevel3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
clickSound.start();
Intent intent = new Intent(LevelPage.this, Level3.class);
startActivity(intent);
}
});
} else {
btnLevel3.setEnabled(false);
}
这是Level2页面,用户在该页面上玩游戏并确保获得35分解锁:
else if (score == 35) {
DBHelper db = new DBHelper(Level2.this);
db.saveData(2,"35");
AlertDialog.Builder builder = new
AlertDialog.Builder(Level2.this);
builder.setTitle("Level 2 Complete!");
builder.setMessage("You have won! You have unlock level 3!
You can proceed to Level 3 or go back.");
builder.setPositiveButton("Unlock", new
DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(Level2.this,
LevelPage.class);
startActivity(intent);
finish();
}
});
这是要保存并获取分数数据的SQLite数据库:
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "Level";
private static final String USERTABLE = "USER_INFO";
private static final String KEY_ID = "id";
private static final String LEVEL1 = "LEVEL1";
private static final String LEVEL2 = "LEVEL2";
private static final String LEVEL3 = "LEVEL3";
private static final String LEVEL4 = "LEVEL4";
private static final String CREATE_TABLE_USER = "CREATE TABLE IF NOT EXISTS "
+ USERTABLE + "(" + KEY_ID + " INTEGER PRIMARY KEY,"
+ LEVEL1 + " TEXT,"
+ LEVEL2 + " TEXT,"
+ LEVEL3 + " TEXT,"
+ LEVEL4 + " TEXT " + ");";
/*private static final String CREATE_TABLE_USER = "CREATE TABLE IF NOT EXISTS "
+ USERTABLE + "(" + KEY_ID + " INTEGER PRIMARY KEY," + LEVEL1 + " TEXT,"
+ LEVEL2 + " TEXT"+");"; */
private static final String SQL_DELETE_ENTRIES =
"DROP TABLE IF EXISTS " + USERTABLE;
public DBHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_TABLE_USER);
}
//to save score in db according to the level
public void saveData(int level,String score){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues data = new ContentValues();
switch(level){
case 1:
data.put(LEVEL1, score);
break;
case 2:
data.put(LEVEL2,score);
break;
case 3:
data.put(LEVEL3,score);
break;
case 4:
data.put(LEVEL4,score);
break;
}
db.insert(USERTABLE,null,data);
db.close();
}
//to get score from db
//you can modify this function if you want to get score for a specific level
public HashMap<String,String> getData(){
HashMap<String,String> scoreMap = new HashMap<>();
String selectQuery = "SELECT * FROM " + USERTABLE ;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) {
do {
scoreMap.put(LEVEL1,cursor.getString(cursor.getColumnIndex(LEVEL1)));
scoreMap.put(LEVEL2,cursor.getString(cursor.getColumnIndex(LEVEL2)));
scoreMap.put(LEVEL3,cursor.getString(cursor.getColumnIndex(LEVEL3)));
scoreMap.put(LEVEL4,cursor.getString(cursor.getColumnIndex(LEVEL4)));
} while (cursor.moveToNext());
}
cursor.close();
return scoreMap;
}
注意:同样适用于3级和4级。即使在4级解锁后,3级也会再次锁定2级。结果会继续重复,它会在下一级解锁,但会锁定前一级突然
放置SharedPrefences:
else if (score == 30) {
/*DBHelper db = new DBHelper(Level1.this);
db.saveData(1,"30"); */
AlertDialog.Builder builder = new
AlertDialog.Builder(Level1.this);
builder.setTitle("Level 1 Complete!");
builder.setMessage("You have won! You have unlock level 2! You can proceed to Level 2 or go back.");
builder.setPositiveButton("Unlock", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(Level1.this, LevelPage.class);
startActivity(intent);
finish();
}
});
builder.setCancelable(false);
builder.show();
runTimer.cancel();
}
答案 0 :(得分:1)
以下是可在我们的案例中使用的SharedPreference的概述
将级别分数写入SharedPreference:
你的saveData()方法将如下所示。
col-lg-offset-5
从SharedPreference读取:
public void saveData(String level, int score) {
SharedPreferences sharedPref = context.getSharedPreferences("score.xml",
Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putInt(getString(level, score); editor.commit();
}
您可以获得有关SharedPreference here的更多信息 修改:
public int getLevelScore(String level) {
SharedPreferences sharedPref = context.getSharedPreferences("score.xml", Context.MODE_PRIVATE);
int levelScore = sharedPref.getInt(level, defaultScoreForLevel);
return levelScore;
}
每当用户得分时调用saveData()方法。