简体   繁体   English

从 TextView 的文本中获取字符串资源名称

[英]Get string resource name from TextView's text

I'd like to get string resource's name from a TextView .我想从TextView获取字符串资源的名称。 So for example I have a string in strings.xml :例如,我在strings.xml中有一个字符串:

<string name="my_text">Hello world</string>

and I set this text to a TextView via XML:我通过 XML 将此文本设置为TextView

<TextView
    android:id="@+id/myAwesomeTextView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/my_text"/>

Now I need to get my string resource's name my_text programatically.现在我需要以编程方式获取字符串资源的名称my_text

I can get text like so:我可以得到这样的文字:

myAwesomeTextView.text //here I get: Hello world

And I can get TextView 's name like so:我可以像这样得到TextView的名字:

resources.getResourceEntryName(myAwesomeTextView.id); //here I get: myAwesomeTextView

Is there a way to get string resource's name - in this case my_text via kotlin/java code有没有办法获取字符串资源的名称 - 在这种情况下my_text通过 kotlin/java 代码

Sorry to say this is not possible.很抱歉,这是不可能的。 TextView never stores the resource id so it's name. TextView 从不存储资源 ID,因此它是名称。 Hope this is helpful.希望这会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM