整数的首选类别是什么?
要在 Android 首选项中存储整数,我会直观地使用 EditTextPreference
并执行通常的 String-int-String 转换。
但后来我遇到了一段代码,它将整数存储在
中:
<ListPreference
android:key="@string/total_score"
android:defaultValue="0" />
并使用 preferences.getInt(getString(R.string.total_score), 0) 检索它;
这真的有效吗?如果是这样,怎么办?
这被认为是可接受的做法吗?
更新:感谢下面的答案,我已经能够找到 getInt()。我将其发布在这里以方便参考:
jint android::content::SharedPreferences::getInt(local_ref< java::lang::String > const &a0, jint a1)
{
return call_method<
android::content::SharedPreferences::J2CPP_CLASS_NAME,
android::content::SharedPreferences::J2CPP_METHOD_NAME(2),
android::content::SharedPreferences::J2CPP_METHOD_SIGNATURE(2),
jint
>(get_jobject(), a0, a1);
}
To store an integer in an Android preference, I would intuitively go for EditTextPreference
and do the usual String-int-String conversions.
But then I came across a piece of code that stores an integer in a <ListPreference>
instead:
<ListPreference
android:key="@string/total_score"
android:defaultValue="0" />
and retrieves it using preferences.getInt(getString(R.string.total_score), 0);
Does this really work? If so, how?
Is it considered acceptable practice?
UPDATE: Thanks to the answers below, I have been able to find the implementation source code for getInt(). I am posting it here for easy reference:
jint android::content::SharedPreferences::getInt(local_ref< java::lang::String > const &a0, jint a1)
{
return call_method<
android::content::SharedPreferences::J2CPP_CLASS_NAME,
android::content::SharedPreferences::J2CPP_METHOD_NAME(2),
android::content::SharedPreferences::J2CPP_METHOD_SIGNATURE(2),
jint
>(get_jobject(), a0, a1);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
理论上,是的,您可以使用
ListPreference
存储整数。毕竟,这是一个 UI 首选项,它将用户显示的标签/键 (android:entries
) 映射到内部值 (android:entryValues
),并在列表视图。该内部值也可能是一个整数。您可以使用
-resource 作为entryValues。实际上,我从未见过这样的工作 - 它有问题.
当然,您可以在代码中为
ListPreference
的首选项键设置一个 int 值,因为它在内部是一个正常的首选项。但这将违背预定义资源数组和从列表中进行选择的能力的全部目的。作为解决方法,如果 int 数组很方便,我建议使用
作为值,并将它们转换为代码中的整数,就像使用EditTextPreference 一样
检查此问题对于一个非工作示例。 ;)
要回答您的标题问题,为整数选择哪一个:视情况而定。
您可以使用任一解决方法。
如果用户应该能够输入任何值(或只是很多值),那么
EditTextPreference
就是最佳选择。ListPreference
太长了。如果您有一小组预定义的整数,请使用
ListPreference
。这样使用起来更舒适,并且可能会显示有用的标签。示例:如果用户应该选择一个时间间隔,您可以将秒映射到值中并显示不同的标签,例如一小时[值 3600;标签“小时”]。编辑:还知道您的代码片段可能与何处相关。由于此
ListPreference
既未指定android:entries
也未指定android:entryValues
,因此它可能只是默认首选项文件的一部分。您可以使用PreferenceManager.setDefaultValues()
使用 XML 文件来重置/初始化所有首选项键。在这种情况下,您选择的首选项完全是随机的,因为所有重要的字段都是android:key
和android:defaultValue
。在这种情况下,您可以使用任何其他类型,这并不重要。In theory, yes you can store an integer with a
ListPreference
. After all it's a UI-preference that maps a user displayed label/key (android:entries
) to an internal value (android:entryValues
) and displays all those mapping options in a listview. That internal value might as well be an integer. You could use a<integer-array>
-resource for the entryValues.In practice, I've never seen that work - it's bugged.
Of course, you can set a int value to the preference key of the
ListPreference
in your code, since it's a normal preference internally. But that would defeat the whole purpose of predefined resource arrays and ability to select from a list. As a workaround, if a int-array would be handy, I recommend using a<string-array>
for the values and convert them an integer in code, as you would with yourEditTextPreference
Check this question for a non-working example. ;)
To answer your title question which one to choose for an integer: Depends.
You can use either one with the workarounds.
If the user should be able to enter any value (or just a lot of values), the
EditTextPreference
is the way to go. AListPreference
would just be too long.If you have a small set of predefined ints, use a
ListPreference
. Thats way more comfortable to use and might be displayed with useful labels. Example: If the user is supposed to select a timing interval, you could map the seconds in the value and display a different label, e.g. an hour [value 3600; label "Hour"].Edit: Also got an idea where your code snippet may be related to. Since this
ListPreference
does neither specifyandroid:entries
norandroid:entryValues
, it might just be part of a default preference file. You can usePreferenceManager.setDefaultValues()
with an XML file to reset/initalize all your preference keys. In this case it's completely random which preference you choose, because all fields that count areandroid:key
andandroid:defaultValue
. You might use any other type in this case, does not matter.我认为发生的事情是
ListPreference
将条目值存储为字符数组,或者换句话说,字节数组(我从 setEntryValues(CharSequence[] entryValues) 方法)。这可能就是存储数字有效的原因,因为该值存储在字节数组中,因此使用preferences.getInt(...)
方法仍然有效。然而,这可能很危险,因为可能会发生签名差异,所以我不推荐它,而是会从字符串首选项中解析 int 。I think what's going on is that the
ListPreference
is storing the entry values as an array of chars, or in other words an array of bytes (I'm inferring this from the setEntryValues(CharSequence[] entryValues) method). This could be why storing a number works, because the value is stored into the byte array, so using thepreferences.getInt(...)
method will still work. This could be dangerous, however, as discrepancies with signing could occur, so I wouldn't recommend it, and would parse the int from a string preference instead.