【问题标题】:Getting a string value from the strings.xml resource file and set it in an interface class从 strings.xml 资源文件中获取字符串值并将其设置在接口类中
【发布时间】:2018-12-13 17:41:04
【问题描述】:

我的代码

String username = "@string/default_username";

default_username 是字符串文件中的键名。

问题是当我尝试在改造中调用 username 时,它无法从字符串文件中获取值,而只是打印 @string/default_username 而不是获取里面的值。

是否可以获取键值并在接口类中设置?

【问题讨论】:

  • 我这样做的原因是我使用productFlavour
  • 为什么不将上下文传递给您的界面并获取字符串?你能分享一下你的界面代码吗?

标签: android string interface


【解决方案1】:

如果你试图访问strings.xml文件上的一个字符串,你需要通过getString(R.string. default_username);获取它

在你的情况下,

String username = getString(R.string. default_username);

【讨论】:

    猜你喜欢
    • 2012-09-20
    • 1970-01-01
    • 1970-01-01
    • 2018-04-20
    • 1970-01-01
    • 1970-01-01
    • 2011-05-20
    • 1970-01-01
    • 2015-06-02
    相关资源
    最近更新 更多