我正在创建一个游戏,其中为用户提供每个问题的提示,当用户按下提示按钮时,提示计数必须减少 1,我有很多具有相同类型逻辑的活动。如何编辑数据并将其获取到我们想要的任何地方。请帮帮我
问问题
59 次
2 回答
1
You could just keep count value in a static variable inside your Application
class.
In your AndroidManifest.xml
you define.-
<application
android:allowBackup="true"
android:name=".YourApplicatinClass"
...
Then define YourApplicationApplication
class like.-
public class YourApplicationClass extends Application {
public static int cont = 0;
}
And access cont
value whenever you need with
YourApplicationClass.cont
于 2013-10-05T14:33:10.003 回答
1
You should just save it in SharedPreferences. Have a look at this Question, that should give you the hint how to work with it. You can write a static method to read an decrement the value saved in there
class Activity1{
onClickListener(){
GlobalSettings.getHits(context)
}
}
class Activity2{
onClickListener(){
GlobalSettings.getHits(context)
}
}
class GlobalSettings{
private static String PREFS_NAME = "myprefs";
private static String PREF_HITS = "hits";
private static int START_VALUE = 10;
public static int getHits(Context context){
SharedPreferences settings = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
return settings.getInt(PREF_HITS, START_VALUE);
}
public static void incrementHits(Context context){
SharedPreferences settings = getSharedPreferences(PREFS_NAME , 0);
SharedPreferences.Editor editor = settings.edit();
editor.putInt(PREF_HITS, getHits(context) + 1);
editor.commit();
}
public static void decrementHits(Context context){
SharedPreferences settings = getSharedPreferences(PREFS_NAME , 0);
SharedPreferences.Editor editor = settings.edit();
editor.putInt(PREF_HITS, getHits(context) - 1);
editor.commit();
}
}
于 2013-10-05T14:33:19.680 回答