Android - 如何在代码中设置首选项
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/552070/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android - How Do I Set A Preference In Code
提问by Will
I have an Android application in which I have my preferences in an XML file, which works fine. I now want to set one of the preferences using code instead of displaying the entire preference screen, how would I go about doing this?
我有一个 Android 应用程序,其中我在 XML 文件中有我的首选项,它工作正常。我现在想使用代码设置其中一个首选项,而不是显示整个首选项屏幕,我将如何执行此操作?
回答by Will
I assume by preferences you are referring to your application's preferences and not Android phone settings.
我假设根据偏好,您指的是应用程序的偏好,而不是 Android 手机设置。
To store preferences between runs of you application you need to do the following
要在应用程序运行之间存储首选项,您需要执行以下操作
Create a SharedPreferences object
SharedPreferences settings = getSharedPreferences(String n, MODE_PRIVATE);
String n identifies your preferences and the second argument is the mode they'll be accessed
Instantiate an Editor object
SharedPreferences.Editor editor = settings.edit();
Note: do not try settings.editor.edit(), this will not make a persistent object and the code below will not work
Write your preferences to the buffer
editor.put...(String, value)
There are numerous put function, putString, putBoolean, etc. The String is the key ("version", "good run") and the value is the value ("1.5.2", true)
Flush the buffer
editor.commit();
This actually writes you put to the preferences. If your app crashes before this line then the preferences will not be written. There is also a documented bug: commit() is supposed to return a boolean indicating success or failure. Last I checked it always returned false.
创建一个 SharedPreferences 对象
SharedPreferences settings = getSharedPreferences(String n, MODE_PRIVATE);
字符串 n 标识您的首选项,第二个参数是它们将被访问的模式
实例化一个 Editor 对象
SharedPreferences.Editor editor = settings.edit();
注意:不要尝试 settings.editor.edit(),这不会产生持久对象,下面的代码也不起作用
将您的首选项写入缓冲区
editor.put...(String, value)
有很多put函数,putString,putBoolean等,String是key(“version”,“good run”),value是值(“1.5.2”,true)
刷新缓冲区
editor.commit();
这实际上将您写入首选项。如果您的应用程序在此行之前崩溃,则不会写入首选项。还有一个记录的错误:commit() 应该返回一个指示成功或失败的布尔值。最后我检查它总是返回false。
These preferences will by stored on the phone and will only be accessible to your application.
这些首选项将存储在手机上,并且只能由您的应用程序访问。
More documentation is here
更多文档在这里
回答by Adan
I tried this but didn't work:
我试过这个但没有用:
SharedPreferences settings = getSharedPreferences(String n, MODE_PRIVATE);
Try this instead:
试试这个:
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
回答by AndroidSter
You can save something in the sharedpreferences by using below code
您可以使用以下代码在共享首选项中保存一些内容
public static void save(String valueKey, String value) {
SharedPreferences prefs = PreferenceManager
.getDefaultSharedPreferences(context);
SharedPreferences.Editor edit = prefs.edit();
edit.putString(valueKey, value);
edit.commit();
}
To read preferences:
要阅读首选项:
public static String read(String valueKey, String valueDefault) {
SharedPreferences prefs = PreferenceManager
.getDefaultSharedPreferences(context);
return prefs.getString(valueKey, valueDefault);
}