2013-03-10 84 views
0

我在屏幕上有一個以100開頭的計數器,當它點擊時,它會減少一個。 我想,當我將關閉應用程序,然後再次打開它時,計數器將保持像以前一樣,而不是將重置爲100應用程序關閉後保存int值

(很抱歉的英文不好...)

+0

使用'SharedPreferences'因爲這將是去 – mango 2013-03-10 10:07:11

+0

@itay lael保存sharedPreferences的方式'onDestroy'並獲得'onCreate'中的值... – 2013-03-10 10:14:19

+0

@TGMCians:在這種情況下,你不應該推薦使用'onDestroy()'。有時候,它永遠不會被調用。來源:http://developer.android.com/reference/android/app/Activity.html#onDestroy() – 2013-03-10 10:17:59

回答

1

您可以使用SharedPreferences來實現你想要的。設置count,同時退出應用程序,重新打開應用程序時,從該應用程序獲取應用程序。

例如關於如何使用它,請檢查此out

+0

謝謝! 幫了我很多! 只是另一個小問題,即時寫這個? onCreate或onPuse? – 2013-03-10 10:11:44

+0

@itaylael:存放在'onPause'並將其放回'onCreate' – Swayam 2013-03-10 10:15:00

1

你可以做這樣的事情:

onPause(),使用此代碼計數器的值保存到SharedPreference文件

SharedPreferences sharedPrefs = getApplicationContext().getSharedPreferences(PREFERENCE_FILE_NAME, Context.MODE_PRIVATE); 
Editor editor = sharedPrefs.edit(); 
editor.putInt(KEY_NAME, THE_INTEGER_VALUE); 
// Replace `putInt` with `putString` if your value is a String and not an Integer. 
editor.commit(); 
  1. 替換上面使用的PREFERENCE_FILE_NAME選擇XML文件將被創建以存儲該值。
  2. KEY_NAME以上使用的是將用於訪問的KEY(用於保存和讀取SharedPreference文件名d。)它是SharedPreferences中使用的鍵值對的一部分。
  3. THE_INTEGER_VALUE是實際值。

而在onResume(),你可以retreive值回並顯示:

SharedPreferences sharedPrefs = getApplicationContext().getSharedPreferences(PREFERENCE_FILE_NAME, Context.MODE_PRIVATE); 
int counter = sharedPrefs.getInt(KEY_NAME, 0); 
// Replace the `int counter` with `String counter` if your value is a String and not an Integer. 
// Also, replace the `getInt` with `getString` 

可以使用int counter後來在TextView也許是爲了顯示。

+0

真棒! 你是偉大的人,謝謝 – 2013-03-10 10:14:50

+0

@itaylael:不客氣。 ;-) – 2013-03-10 10:18:38

+0

老兄我還有一個問題,我沒有低頭....如果我把SharedPreferences字符串值如何我從它得到我的計數值? 和字符串值應該是特別的東西還是隻寫在它的東西? – 2013-03-11 17:03:41

0

您需要使用這種方式SharedPreferences保存的值,然後再讓它

private final String NUMBER = "Number"; 
private final String PROFILE = "Profile"; 

SharedPreferences a = FirstActivity.this.getSharedPreferences("a", MODE_PRIVATE); 
SharedPreferences.Editor prefsEditorProfiles = a.edit(); 
prefsEditorProfiles.putInt(Profile, 1); 
prefsEditorProfiles.putInt(Number, 1); 
prefsEditorProfiles.commit(); 

然後在其他Activity恢復SharedPreferences

SharedPreferences a = SecondActivity.this.getSharedPreferences("a", MODE_PRIVATE); 
int ab = a.getInt(Number, 0); 
0

使用份額的偏好作爲doc example讀取/寫入int值的變化:

public class Calc extends Activity { 
    public static final String PREFS_NAME = "MyPrefsFile"; 

    @Override 
    protected void onCreate(Bundle state){ 
     super.onCreate(state); 
     . . . 

     // Restore preferences 
     SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0); 
     int lastIndex = settings.getInt("yournumbername", 100); 
     setLastIndex(lastIndex); 
    } 

    @Override 
    protected void onStop(){ 
     super.onStop(); 

     // We need an Editor object to make preference changes. 
     // All objects are from android.context.Context 
     SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0); 
     SharedPreferences.Editor editor = settings.edit(); 
     editor.putInt("yournumbername", mlastIndex); 

     // Commit the edits! 
     editor.commit(); 
    } 
} 
相關問題