简体   繁体   English

动态查询一个String是Strings.xml文件

[英]Dynamically querying a String is Strings.xml file

Normally i am using getString(R.string.) to query strings in xml file.通常我使用 getString(R.string.) 来查询 xml 文件中的字符串。 Is there any way to query xml file via getString("").有没有办法通过getString("")查询xml文件。 Because in my code i want to load the string present in resource file dynamically according to the condition.因为在我的代码中,我想根据条件动态加载资源文件中存在的字符串。 Any help would be really useful for me in learning android.任何帮助都对我学习 android 非常有用。

-Regards, Ron.. -问候,罗恩..

You can retrieve the id of a string resource dynamically as follows:您可以动态检索字符串资源的 id,如下所示:

int resId = getResources().getIdentifier("key_name", "string", getPackageName());
Log.d("MyApp", "resource id is " + resId + " and value is " + getString(resId));

Where key_name is the name of a string in your strings.xml.其中 key_name 是您的字符串中的字符串名称。xml。

I hope I have understood your question correctly.我希望我正确理解了你的问题。

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

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