如何强制元数据值键入字符串? [英] How to force a meta-data value to type string?
问题描述
我在manifest.xml中指定了一个元数据,如下所示:
I specified a meta-data in my manifest.xml like this:
<meta-data android:value="5555555555" android:name="foo"></meta-data>
当访问这样的元数据时:
When accessing the meta data like this:
ActivityInfo ai = act.getPackageManager().getActivityInfo(componentName, PackageManager.GET_META_DATA);
Object value = (Object)ai.metaData.get(key);
此值被解释为int,更重要的是,其解释不正确(1260588259).
this value gets interpreted as int and - more importantly - incorrectly interepreted (1260588259).
由于类型是由构建系统自动确定的(布尔值,整数,浮点数和字符串),所以我想知道是否有任何方法可以将数据类型强制为字符串.
Since the type is determined automatically by the build system ( booleans, ints, floats and strings ) i wondered if there is any way to force the datatype to string.
我尝试在末尾添加一个空格("5555555555"),但是随后该值被解释为5.5555553E9 float! ).
I tried adding a space at the end ( "5555555555 "), but then the value gets interpreted as 5.5555553E9 float! ).
我也尝试使用getString代替get,但是返回null.
I also tried using getString instead of get, but then null is returned.
有什么想法吗? TIA.
Any ideas? TIA.
推荐答案
在数字前放置一个转义的空格似乎可行:
Putting an escaped space in front of the number seems to work:
<meta-data android:name="foo" android:value="\ 1234567890"/>
转义的空格不会出现在get()
返回的值中.
The escaped space doesn't appear in the value returned by get()
.
老实说,我不知道它是如何工作的.
I have no idea quite how this works, if I'm honest.
如果您要有记载的东西,因此还有更多可靠,您始终可以将字符串值作为资源并引用它:
If you want something documented and therefore more reliable you can always put your string value as a resource and refer to it:
<meta-data android:name="foo" android:resource="@string/mynumber"/>
这篇关于如何强制元数据值键入字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!