Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有这个代码
Random x = new Random(44); int l = x.nextInt(); int path = R.raw.l;
我知道它不会那样工作,但它最能解释我的情况。即提供 l==5,我想使用 R.raw.5 作为路径。但是,R.raw 似乎只理解简单的值,而不是引用的值。请帮忙!
您可以int使用以下代码从您的活动中获取标识符:
int
Random x = new Random(44); int l = x.nextInt(); int path = getResources().getIdentifier(String.valueOf(l), "raw", getPackageName());
文档中的更多信息:http: //developer.android.com/reference/android/content/res/Resources.html#getIdentifier%28java.lang.String,%20java.lang.String,%20java.lang.String%29