Android PreferenceActivity:将值保存为整数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3721358/
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
PreferenceActivity: save value as integer
提问by Laimoncijus
Using a simple EditTextPreference
in my preferences activity:
EditTextPreference
在我的偏好活动中使用一个简单的:
<EditTextPreference
android:key="SomeKey"
android:title="@string/some_title"
android:summary="..."
android:numeric="integer"
android:maxLength="2"
/>
Is there a way that this configuration value would be saved as integer? Seems now it just allows to enter numbers, but the value is still saved as string:
有没有办法将此配置值保存为整数?现在似乎它只允许输入数字,但该值仍保存为字符串:
Calling:
调用:
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
int value = preferences.getInt("SomeKey", -1);
throws me java.lang.ClassCastException: java.lang.String
, and:
抛出我java.lang.ClassCastException: java.lang.String
,并且:
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
String value = preferences.getString("SomeKey", "-1");
retrieves the value successfully.
成功检索值。
How to make PreferenceActivity
to save value as integer by default?
PreferenceActivity
默认情况下如何将值保存为整数?
回答by broot
You could extend EditTextPreference:
您可以扩展 EditTextPreference:
public class IntEditTextPreference extends EditTextPreference {
public IntEditTextPreference(Context context) {
super(context);
}
public IntEditTextPreference(Context context, AttributeSet attrs) {
super(context, attrs);
}
public IntEditTextPreference(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected String getPersistedString(String defaultReturnValue) {
return String.valueOf(getPersistedInt(-1));
}
@Override
protected boolean persistString(String value) {
return persistInt(Integer.valueOf(value));
}
}
It would be better to overwrite onSetInitialValue() and setText() methods, but then you would have to copy some code from a base class. Above solution is simplier, but it's quite tricky - "string" methods do something with ints. Try to not extend this class further ;-)
最好覆盖 onSetInitialValue() 和 setText() 方法,但是您必须从基类复制一些代码。上面的解决方案更简单,但它非常棘手 - “字符串”方法对整数做一些事情。尽量不要进一步扩展这个类;-)
You could use it from XML by:
您可以通过以下方式从 XML 使用它:
<package.name.IntEditTextPreference
android:key="SomeKey"
android:title="@string/some_title"
android:summary="..."
android:numeric="integer"
android:maxLength="2"
/>
回答by broot
Even if you set android:numeric="integer" it'll be text preference - as its name suggest. You could easily convert string value to int using Integer.valueOf(). Also you could overwrite PreferenceActivity to do conversion automatically on exit.
即使您设置了 android:numeric="integer" 它也将是文本首选项 - 顾名思义。您可以使用 Integer.valueOf() 轻松地将字符串值转换为 int。您也可以覆盖 PreferenceActivity 以在退出时自动进行转换。
I think the best solution is to write simple method to get this value from preferences. Something like:
我认为最好的解决方案是编写简单的方法来从偏好中获取这个值。就像是:
public static int getSomePref(Context context) {
SharedPreferences prefs =
PreferenceManager.getDefaultSharedPreferences(context);
String value = prefs.getString("SomeKey", null);
return value == null ? -1 : Integer.valueOf(value);
}
Then you could very easily use it from your code.
然后你可以很容易地从你的代码中使用它。
回答by Arnab Jain
Even though an Answer has been parked accepted I would like to share one more shorter way to achieve this :
即使已经接受了一个答案,我还是想分享一种更短的方法来实现这一目标:
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
int value = Integer.parseInt(preferences.getString("SomeKey", "-1"));
Since you have already set that only numbers can be entered this won't through any exception. yet to complete my answer :
由于您已经设置只能输入数字,因此不会出现任何异常。尚未完成我的回答:
<EditTextPref
android:key="SomeKey"
android:title="@string/some_title"
android:summary="..."
android:numeric="integer"
android:maxLength="2" />
回答by Jadeye
I think this is the shortest one I could come up with:
我认为这是我能想到的最短的一个:
int CheckInterval = Integer.parseInt(sharedPreferences.getString("check_frequency","60"));
回答by cartok
I had the same Problem. (I wanted SharedPreference to give me a port number that i stored in a preferences xml file as defaultValue).
我有同样的问题。(我希望 SharedPreference 给我一个端口号,我将其作为 defaultValue 存储在首选项 xml 文件中)。
Implementing all the SharedPreferences methods would be much effort, so writing a custom method in the class that instanced the SharedPreferences, as brootsuggested would be best i think.
实现所有 SharedPreferences 方法会很费力,所以在实例化 SharedPreferences 的类中编写一个自定义方法,正如broot建议的那样,我认为最好。
You can aswell just use the Static method of Integer in the line where you need it:
您也可以在需要的行中使用 Integer 的静态方法:
int number = Integer.valueOf(settings.getString("myNumberString", "0"));