ユーザーがすべての質問に対してヒントを与えられるゲームを作成しています。ユーザーがヒント ボタンを押すと、ヒント カウントが 1 減らなければなりません。同じタイプのロジックを持つアクティビティがたくさんあります。データを編集して、必要な場所に取得する方法。私を助けてください
2 に答える
1
Application
クラス内の静的変数にカウント値を保持するだけです。
あなたAndroidManifest.xml
が定義する.-
<application
android:allowBackup="true"
android:name=".YourApplicatinClass"
...
次に、次のようYourApplicationApplication
なクラスを定義します。
public class YourApplicationClass extends Application {
public static int cont = 0;
}
cont
必要なときにいつでも価値にアクセスできます
YourApplicationClass.cont
于 2013-10-05T14:33:10.003 に答える
1
SharedPreferencesに保存するだけです。このQuestionを見てください。これにより、操作方法のヒントが得られるはずです。そこに保存された値のデクリメントを読み取る静的メソッドを作成できます
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 に答える