2016-12-27 67 views
-1

在我的新應用中,點擊按鈕時計數器增加。我想用sharedPreferences保存高分,所以分數會保存下來並在下次啓動時顯示。問題是,即使有其他回答的問題,我也沒有真正做到。android - 用sharedPreferences保存int

package com.example.test; 

public class MainActivity extends ActionBarActivity { 

    public int score = 0; 
    public int highscore = 0; 
    TextView tvscore; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     TextView tvhighscore= (TextView) findViewById(R.id.highscore); 
     tvscore = (TextView) findViewById(R.id.score); 
     Button count = (Button) findViewById(R.id.button1); 

     tvhighscore.setText(String.valueOf(highscore)); 

     SharedPreferences prefs = this.getSharedPreferences("score", Context.MODE_PRIVATE); 
     Editor editor = prefs.edit(); 
     editor.putInt("score", 0); 
     editor.commit(); 



    } 

    public void onClick (View view) { 
     score++; 
     tvscore.setText(String.valueOf(score)); 

     SharedPreferences prefs = this.getSharedPreferences("myPrefsKey", Context.MODE_PRIVATE); 
     int highscore = prefs.getInt("score", 0); 
    } 


} 
+0

需要在'SharedPreferences'中點擊按鈕保存評分 –

+0

@яятьянаякомпанияK是正確的。另外請確保您使用共享首選項文件的相同名稱。 –

+0

我是不是使用同一個名字'「得分」'? –

回答

0

首先,在編寫和查詢時,您需要使用相同的密鑰作爲共享首選項。然後在onclick中,您需要將分數存儲在首選項中,而不是再次查詢。下面是更新後的代碼:

public class MainActivity extends ActionBarActivity { 

     public int score = 0; 
     public int highscore; 
     TextView tvscore, tvhighscore; 

     @Override 
     protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity_main); 

      tvhighscore= (TextView) findViewById(R.id.highscore); 
      tvscore = (TextView) findViewById(R.id.score); 
      Button count = (Button) findViewById(R.id.button1); 


      SharedPreferences prefs = this.getSharedPreferences("myPrefsKey", Context.MODE_PRIVATE); 
      highscore = prefs.getInt("high_score", 0); 
      tvhighscore.setText(String.valueOf(highscore)); 
     } 

     public void onClick (View view) { 
      score++; 
      tvscore.setText(String.valueOf(score)); 
      if (score > highscore) { 
       highscore = score; 
       SharedPreferences prefs = this.getSharedPreferences("myPrefsKey", Context.MODE_PRIVATE); 
       prefs.edit().putInt("high_score", highscore).apply(); 
       tvhighscore.setText(String.valueOf(highscore)); 
      } 
     } 
    } 
+0

它的工作原理,謝謝! –

0

你有一些錯誤,在你的代碼

第一個錯誤是,你正在使用不同的名稱爲SP

SharedPreferences通過​​進行訪問。在你的代碼中你有兩個:myPrefsKeymyPrefsKey。確保始終使用相同的值,否則將不會找到該值。

第二個是,你

無論是在代碼和onclick方法,則需要使用相同的名稱highscoreint使用兩次相同INT名。這是不允許的

三是邏輯:

什麼,你正在做的是:

  • activity開始保存價值。在button點擊

  • 閱讀價值的同時,你應該做到以下幾點:

    • 使用您正在使用的button點擊裏面調用getInt代碼onCreate方法讀取的值和設定textview's文本它。
    • 在遞增後保存button點擊的值。

    這樣你就可以使代碼工作。

    下面的例子:

    package com.example.test; 
    
    public class MainActivity extends ActionBarActivity { 
    
        public int score = 0; 
        public int highscore = 0; 
        TextView tvscore; 
        SharedPreferences prefs; 
    
        @Override 
        protected void onCreate(Bundle savedInstanceState) { 
         super.onCreate(savedInstanceState); 
         setContentView(R.layout.activity_main); 
    
         TextView tvhighscore= (TextView) findViewById(R.id.highscore); 
         tvscore = (TextView) findViewById(R.id.score); 
         Button count = (Button) findViewById(R.id.button1); 
         //here you retrieve the value of the highscore 
         prefs = this.getSharedPreferences("myPrefsKey", Context.MODE_PRIVATE); 
         int highscore = prefs.getInt("score", 0); 
         tvhighscore.setText(String.valueOf(highscore)); 
    
    
    
        } 
    
        public void onClick (View view) { 
         score++; 
         //here you save the value of the score in your pref 
         tvscore.setText(String.valueOf(score)); 
         Editor editor = prefs.edit(); 
         editor.putInt("score", score); 
         editor.commit(); 
        } 
    
    } 
    

    說不上來,如果這正是你要找的,但這應該幫助你理解的邏輯:)

    祝你好運!

  • +0

    我會試試看,謝謝! –

    0

    我建議你創建一個類來管理你的sp。 我在下面給你舉個例子。

    public class SharedPrefsManager { 
        private static final String USER_CODE = "userCode"; 
        private static SharedPreferences sharedPreferences; 
        private static SharedPreferences.Editor prefEditor; 
    
        private static void setPreferences(Context context) { 
         if (context == null) { 
          context = Application.getContext(); 
         } 
         sharedPreferences = context.getSharedPreferences("APP_NAME", 0); 
        } 
    
        public static int getCodigoUsuario(Context context) { 
         setPreferences(context); 
         return sharedPreferences.getString(USER_CODE, 0); 
        } 
    
        public static void setCodigoUsuario(int userCode, Context context) { 
         setPreferences(context); 
         prefEditor = sharedPreferences.edit(); 
         prefEditor.putInt(USER_CODE, userCode); 
         prefEditor.commit(); 
        } 
        } 
    

    SAVE:SharedPrefsManager.setCodigoUsuario(13,背景);

    GET SharedPrefsManager.getCodigoUsuario(context);

    +1

    並且在我嘗試去做這個課程時,我會保存它嗎? –

    +0

    SharedPrefsManager.setCodigoUsuario(user,context); –