如何永久存储首选项屏幕中的值?
我是 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 上设置的值。
你能帮助我理解我做错了什么吗?
非常感谢。
I am a newbie in Android and I'm just trying to permanently store a string.
I want to get this string from a PreferenceActivity
and then update the text of a TextView
.
I was reading about the available options for persistent storage: http://developer.android.com/guide/topics/data/data-storage.html#pref
I've tried to use SharedPreferences
but it's very unclear for me on how it should work.
I have created a very simple test app.
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>
The text of the TextView
never changes, it is always set to the value I set on strings.xml.
Can you please help me to understand what am I doing wrong?
Thank you very much.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您非常接近我的建议:
1)定义一些共享首选项:
2)从共享首选项中读取
3)保存/更新共享首选项:
You're extremely close to what I'd recommend:
1) Define some shared preferences:
2) Read from shared preferences
3) Save/update a shared preferences:
将用户名、密码保存/写入首选项的快速示例。
从首选项中读取用户名、密码的快速示例。
Quick example of saving/writing username, password into preferences.
Quick example of reading username, password from preferences.
我刚刚意识到与
SharedPreferences
相关的代码是正确的,但TextView
永远不会更新,因为我尝试直接编辑字符串本身,而不是TextView
!就这样解决了:
非常感谢你们的帮助:)
I just realized my code related to
SharedPreferences
is correct, but theTextView
never updates because I tried to edit directly the string itself, not theTextView
!Just resolved this way:
Many thanks for your help guys :)