java 如何从 String 中获取 Resource(int) - Android

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10170904/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 23:54:46  来源:igfitidea点击:

How to get Resource(int) from String - Android

javaandroidstringresourcesint

提问by Saruulbat

Possible Duplicate:
Android, getting resource ID from string?

可能的重复:
Android,从字符串中获取资源 ID?

String res = "R.drawable.image1";

I need to get it's INT id. Please help me!

我需要得到它的INT ID。请帮我!

回答by Nishant

Use this to get resource id:

使用它来获取资源 ID:

int picId = getResources().getIdentifier(picName, "drawable", getApplicationContext().getPackageName());

回答by Michal

If you want to get the integerthen simply remove the quotes.

如果你想得到integer然后只需删除引号。

int id = R.drawable.image1; // instead of "R.drawable.image1"

That will give you the number.

那会给你号码。

回答by Chandra Sekhar

View view = findViewById(R.drawable.image1);
int id = view.getId();