gpt4 book ai didi

android - Resources.getIdentifier(),deftype 参数的可能值?

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:24:51 27 4
gpt4 key购买 nike

我正在尝试了解一款对投资组合进行计算的 Android 应用。投资组合存储在 res/values/portfolio.xml 中:

enter image description here

当在应用程序中按下一个按钮时,投资组合数据被检索如下:

String portfolioName = ((TextView) findViewById(R.id.portfolioName)).getText().toString();
Resources res = getResources();
String[] data = res.getStringArray(res.getIdentifier(portfolioName, "array", this.getPackageName()));

我在 String Array 上找到了 Android 文档解释 portfolio.xml 语法的资源类型文件,它解释了为什么 name属性应用作 getIdentifier() 的第一个参数:

“The filename is arbitrary. The <string-array> element's name will be used as the resource ID.”

但我还没有找到任何文档来说明您如何知道应该为 defType 放置什么? getIdentifier 的参数(除此之外它是一个字符串)。在提供的示例中,"array"有效,但它来自哪里?一般而言,“defType”的可能值是什么?

最佳答案

getIdentifier 返回给定资源名称的资源的 id。 typeDef 指的是 Resource 的类型(阅读更多 here )。请记住,res 的内容是在编译时解析的,R.java 类是根据解析结果生成的。最后,您要查找的是在该类中声明的字段。我不知道内部实现,但如果你提供 array 作为 res 类型,android 将只查找 R.array,而不是整个 R

关于android - Resources.getIdentifier(),deftype 参数的可能值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35756896/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com