如何从资源id获取资源名

时间:2022-12-07 21:16:26


In my layout I have defined something like this .

在我的布局中,我定义了这样的东西。

<RadioButton
    android:id="@+id/radio1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Dnt want this text" />

Assume that some function in activity returns me this id (id of radioButton). Now i want to get this text radio1 from this id. In short I want to retrieve text radio1 written in android:id="@+id/radio1"

假设活动中的某个函数返回这个id (radioButton的id)。现在我想从这个id中获取这个文本radio1,简言之,我想检索用android写的文本radio1:id="@+id/radio1"

Can somebody tell me how is it possible ?

有人能告诉我怎么可能吗?

5 个解决方案

#1


273  

In your Activity, try these:

在你的活动中,试试以下方法:

  1. to get string like radio1:

    要得到像radio1这样的字符串:

    getResources().getResourceEntryName(int resid);
    
  2. to get string like com.sample.app:id/radio1:

    如com.sample.app:id/radio1:

    getResources().getResourceName(int resid);
    

In Kotlin Now :

现在在芬兰湾的科特林:

val name = v.context.resources.getResourceEntryName(v.id)

#2


8  

You have id('long' type) from that id you want to access radio button id(name) that is radio1. You use this

您有id('long'类型),您想要访问radio1的单选按钮id(名称)。你用这个

getResources().getResourceEntryName(id);

in using above you can get name of radio button i.e. radio1. here parameter id is which you have(long type). Try this it will helps you 100%.

在使用上述软件时,你可以获得单选按钮的名称,即radio1。这里的参数id是您拥有的(长类型)。尝试一下,它会帮助你100%。

#3


1  

If am right what you wanted to retrieved is the word "radio1" (from the id itself?) so if that's the case then first you need to get its id

如果我是对的,你想要检索的是单词“radio1”(来自id本身?)所以如果是这样,那么首先你需要获取它的id

int intname= buttonname.getId();

then get the result of it

然后得到结果

String stringname= get regetResources().getResourceEntryName(intname);

hoped I helped

希望我帮助

#4


0  

You mean you want to take the string text of the id?

你是说你想取id的字符串文本?

Since you have defined it you should know what this is.

既然你已经定义了它,你应该知道它是什么。

If you have a layout and you want to find if a View has a specific id, you can traverse the whole layout and check with getId(), if the id of each View is the id you are looking for..

如果您有一个布局,并且想要查找一个视图是否有一个特定的id,您可以遍历整个布局并与getId()进行检查,如果每个视图的id都是您要查找的id。

Hope this helps (if I have understand correct your question.. :) )

希望这对你有帮助(如果我能理解你的问题。:))

#5


0  

Kotlin:

芬兰湾的科特林:

val name = v.context.resources.getResourceEntryName(v.id)

#1


273  

In your Activity, try these:

在你的活动中,试试以下方法:

  1. to get string like radio1:

    要得到像radio1这样的字符串:

    getResources().getResourceEntryName(int resid);
    
  2. to get string like com.sample.app:id/radio1:

    如com.sample.app:id/radio1:

    getResources().getResourceName(int resid);
    

In Kotlin Now :

现在在芬兰湾的科特林:

val name = v.context.resources.getResourceEntryName(v.id)

#2


8  

You have id('long' type) from that id you want to access radio button id(name) that is radio1. You use this

您有id('long'类型),您想要访问radio1的单选按钮id(名称)。你用这个

getResources().getResourceEntryName(id);

in using above you can get name of radio button i.e. radio1. here parameter id is which you have(long type). Try this it will helps you 100%.

在使用上述软件时,你可以获得单选按钮的名称,即radio1。这里的参数id是您拥有的(长类型)。尝试一下,它会帮助你100%。

#3


1  

If am right what you wanted to retrieved is the word "radio1" (from the id itself?) so if that's the case then first you need to get its id

如果我是对的,你想要检索的是单词“radio1”(来自id本身?)所以如果是这样,那么首先你需要获取它的id

int intname= buttonname.getId();

then get the result of it

然后得到结果

String stringname= get regetResources().getResourceEntryName(intname);

hoped I helped

希望我帮助

#4


0  

You mean you want to take the string text of the id?

你是说你想取id的字符串文本?

Since you have defined it you should know what this is.

既然你已经定义了它,你应该知道它是什么。

If you have a layout and you want to find if a View has a specific id, you can traverse the whole layout and check with getId(), if the id of each View is the id you are looking for..

如果您有一个布局,并且想要查找一个视图是否有一个特定的id,您可以遍历整个布局并与getId()进行检查,如果每个视图的id都是您要查找的id。

Hope this helps (if I have understand correct your question.. :) )

希望这对你有帮助(如果我能理解你的问题。:))

#5


0  

Kotlin:

芬兰湾的科特林:

val name = v.context.resources.getResourceEntryName(v.id)