Android, getting resource ID from string?

后端 未结 14 1699
忘掉有多难
忘掉有多难 2020-11-21 23:56

I need to pass a resource ID to a method in one of my classes. It needs to use both the id that the reference points to and also it needs the string. How should I best achie

14条回答
  •  佛祖请我去吃肉
    2020-11-22 00:05

    Since you said you only wanted to pass one parameter and it did not seem to matter which, you could pass the resource identifier in and then find out the string name for it, thus:

    String name = getResources().getResourceEntryName(id);
    

    This might be the most efficient way of obtaining both values. You don't have to mess around finding just the "icon" part from a longer string.

提交回复
热议问题