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

标签 android

我正在尝试了解一款对投资组合进行计算的 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/

相关文章:

android - 让服务接收 SMS 消息

java - 多次使用 MediaRecorder

android - 将相机预览旋转到Portrait Android OpenCV Camera和Xamarin

java - 当用户按下按钮时手机振动时如何实现?

android - 如何从在线数据库获取最新的最后一行并每 10 秒刷新一次(Kinvey)

android - 找不到 Gradle 构建错误样式属性

Android 持久 Bottom Sheet 初始可见性

android - 用户输入最后一位数字后立即进行密码验证

android - 移动应用程序中的审核日志记录

java - 接收电子邮件