如何通过其值获取字符串资源名称

时间:2020-12-17 23:38:20

I want to get a string resource name or ID by passing its value.
Example :

我希望通过传递其值来获取字符串资源名称或ID。示例:

    <string name="stringName">stringValue</string>

I want to pass the stringValue and get the stringName or ID (id value)

我想传递stringValue并获取stringName或ID(id值)

Based on ID i will do some calculation to get another ID of another String resource

基于ID,我将进行一些计算以获得另一个String资源的另一个ID

2 个解决方案

#1


0  

I don't know if that's possible and I think the IDs can change without warning if the R class gets newly generated. You could of course try some reflection magic on this class, but I would recommend against it.

我不知道这是否可能,我认为如果新生成R类,ID可以在没有警告的情况下改变。你当然可以在这堂课上尝试一些反思魔法,但我会反对它。

#2


0  

I also have this problem, and can think of 2 possible workarounds:

我也有这个问题,可以想到2种可能的解决方法:

  • load all the strings and their names into a table, and look in the table.
  • 将所有字符串及其名称加载到表中,然后查看表格。
  • Or cycle through my complete list of names, getting the string resource for each one, and comparing it to my known string resource.
  • 或者遍历我的完整名称列表,获取每个名称的字符串资源,并将其与我已知的字符串资源进行比较。

I am implementing the 2nd one, as my list of string resources is not very big, and I don't have to do this operation very often. Once the name is known, it's possible to get the Resource Id via:

我正在实现第二个,因为我的字符串资源列表不是很大,我不必经常做这个操作。知道名称后,可以通过以下方式获取资源ID:

//Get resource id from name
    var resourceId = (int) typeof (MyApp_droid.Resource.String).GetField(MyStringName).GetValue(null);

(code is C# because I'm working in Xamarin).

(代码是C#,因为我在Xamarin工作)。

#1


0  

I don't know if that's possible and I think the IDs can change without warning if the R class gets newly generated. You could of course try some reflection magic on this class, but I would recommend against it.

我不知道这是否可能,我认为如果新生成R类,ID可以在没有警告的情况下改变。你当然可以在这堂课上尝试一些反思魔法,但我会反对它。

#2


0  

I also have this problem, and can think of 2 possible workarounds:

我也有这个问题,可以想到2种可能的解决方法:

  • load all the strings and their names into a table, and look in the table.
  • 将所有字符串及其名称加载到表中,然后查看表格。
  • Or cycle through my complete list of names, getting the string resource for each one, and comparing it to my known string resource.
  • 或者遍历我的完整名称列表,获取每个名称的字符串资源,并将其与我已知的字符串资源进行比较。

I am implementing the 2nd one, as my list of string resources is not very big, and I don't have to do this operation very often. Once the name is known, it's possible to get the Resource Id via:

我正在实现第二个,因为我的字符串资源列表不是很大,我不必经常做这个操作。知道名称后,可以通过以下方式获取资源ID:

//Get resource id from name
    var resourceId = (int) typeof (MyApp_droid.Resource.String).GetField(MyStringName).GetValue(null);

(code is C# because I'm working in Xamarin).

(代码是C#,因为我在Xamarin工作)。