我是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 :(得分: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 :(得分: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);
答案 2 :(得分: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);
非常感谢你们的帮助:)