简体   繁体   English

如何通过Java(Android)更改strings.xml中的值

[英]How to change values in strings.xml through Java (Android)

I'm trying to change the values in my strings.xml using Java. 我正在尝试使用Java更改我的strings.xml中的值。 It's to change certain labels in an app dynamically on certain events. 它是根据某些事件动态更改应用程序中的某些标签。 Any help would be appreciated 任何帮助,将不胜感激

You can't change values in strings.xml as Strings are immutable (unless you do some dynamic compilation). 您不能在strings.xml中更改值,因为字符串是不可变的(除非您进行一些动态编译)。 You can have enums of Strings that can change the answer depending on passed-in variables 您可以具有可根据传入变量更改答案的字符串枚举

I think the best solution for your need would be to define all possible strings within the XML File. 我认为满足您需求的最佳解决方案是在XML文件中定义所有可能的字符串。 Based on the event in your application you can then choose the correct string. 根据您应用程序中的事件,然后可以选择正确的字符串。 If the event is something like a user input then the changing the XML file is not possible. 如果事件类似于用户输入,则无法更改XML文件。 But in this case it should be fine to change the String directly without using XML references. 但是在这种情况下,最好直接更改String而不使用XML引用。

您可能想看一下Java String Formatter,它只是让您在string.xml中格式化一个字符串,而不更改整个String,正如其他答案所说,您不能从以下位置更改strings.xml中的String值您的Java代码。

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

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