2012-01-12 52 views
1

我是Android的新手,我只是試圖永久存儲一個字符串。如何從首選項屏幕永久存儲值?

我想從PreferenceActivity得到這個字符串,然後更新TextView的文本。

我正在讀關於持久性存儲可用的選項:http://developer.android.com/guide/topics/data/data-storage.html#pref

我試着使用SharedPreferences但很不清楚,我就應該是如何工作的。

我創建了一個非常簡單的測試應用程序。

MainActivity.java

public class MainActivity extends Activity { 
/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
} 

/* method that starts preferences */ 
public void openPreferences(View v) { 
    Intent i = new Intent(this, Preferences.class); 
    startActivity(i); 
} 

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

    // Get preferences 
    SharedPreferences preferences = getPreferences(0); 
    String name = preferences.getString("name","Empty string!"); 

    // Create a RemoteViews layout 
    RemoteViews views = new RemoteViews(getPackageName(),R.layout.main);  
    // set new text for labels 
    views.setTextViewText(R.string.name, name); 
} 

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

    // We need an Editor object to make preference changes. 
    // All objects are from android.context.Context 
    SharedPreferences preferences = getPreferences(0); 
    SharedPreferences.Editor editor = preferences.edit(); 
    editor.putString("name", "This is a test"); 
    // Commit the edits! 
    editor.commit(); 
} 
} 

Preferences.java

public class Preferences extends PreferenceActivity { 
     @Override 
     protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      addPreferencesFromResource(R.xml.preferences); 
     } 
} 

的AndroidManifest.xml

<application android:icon="@drawable/icon" android:label="@string/app_name"> 
    <activity android:name=".MainActivity" 
       android:label="@string/app_name"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 
      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
    <activity android:name=".Preferences" 
       android:label="@string/preferences_title"> 
    </activity> 

</application> 

TextView的文字永遠不會改變,它總是被設置爲我在strings.xml中設置的值。

你能幫我理解我做錯了什麼嗎?

非常感謝。

+0

我覺得你與sharedpreferences代碼是正確的。也許與刷新remoteviews有關。你記錄了preferences.getString嗎?它顯示錯誤的價值? – nandeesh 2012-01-12 18:44:42

回答

4

你非常接近我建議你:

1)定義一些共享偏好:

public static final String MY_PREFERENCES = "MyPreferences"; 
    ... 
    public static final SOME_PREFERENCE = "SomePreference"; 

2)共享偏好

SharedPreferences myPreferences; 
    ... 
    myPreferences = getSharedPreferences (MY_PREFERENCES, Context.MODE_PRIVATE); 

3)保存閱讀/更新共享偏好:

Editor editor = myPreferences.edit(); 
    editor.putString (SOME_PREFERENCE, "abc"); 
    editor.commit(); 
1

將用戶名,密碼保存/寫入首選項的快速示例。從喜好閱讀的用戶名,密碼

SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this); 

Editor edit = preferences.edit();   
edit.putString("username", "abcd");   
edit.putString("password", "abcd#");   
edit.commit(); 

簡單的例子。

SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);  

String strUsername = preferences.getString("username", null); 
String strPassword = preferences.getString("password", null); 

0

我才意識到我與SharedPreferences代碼是正確的,但TextView從來沒有更新,因爲我試圖直接編輯字符串本身,而不是TextView

就解決了這個辦法:

/* Update text of TextView */ 
    TextView t = (TextView) this.findViewById(R.id.name); 
    // previously was R.string.name - not correct! 
    t.setText(name); 

非常感謝您的幫助球員:)