java– 在string.xml中调用变量

java– 在string.xml中调用变量,第1张

概述参见英文答案>dynamicStringusingString.xml?                                    9个有关完整代码,请参阅我之前的问题here.如果我想将我的应用程序国际化,我需要为我的项目中的某些语句创建字符串对象.最困扰我的是这个代码块.

参见英文答案 > dynamic String using String.xml?                                    9个
有关完整代码,请参阅我之前的问题here.

如果我想将我的应用程序国际化,我需要为我的项目中的某些语句创建字符串对象.最困扰我的是这个代码块.

    ...    else        Toast.makeText(MainActivity.this,             "Good job! The answer was " + comp + ".\n" +            "You made " + guesses + " guesses.\n" +            "Restart the app to try again.",             Toast.LENGTH_LONG).show();    }};

strings.xml中与此相关的部分如下所示:

<string name="correct">Good job! The answer was + comp\n               You made + guesses + guesses.\n               Restart the app to try again.</string>

我想要comp和猜测变量来显示它们各自的值,但我不知道如何.

我计划对代码块执行此 *** 作

    ...    else        Toast.makeText(MainActivity.this,             R.string.correct,             Toast.LENGTH_LONG).show();    }};

谢谢.

解决方法:

在你的xml文件string.xml中:

<?xml version="1.0" enCoding="utf-8"?><resources>     <string name="correct">Good job! The answer was %1$s\n               You made %2$s guesses.\n               Restart the app to try again.</string></resources>

如果要在文本中添加其他值,则增加%2 $s – > %3 $s等…

然后你可以像在java中一样使用它:

Toast.makeText(MainActivity.this, getString(R.string.correct, comp, guesses),             Toast.LENGTH_LONG).show();
总结

以上是内存溢出为你收集整理的java – 在string.xml中调用变量全部内容,希望文章能够帮你解决java – 在string.xml中调用变量所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1119957.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-29
下一篇2022-05-29

发表评论

登录后才能评论

评论列表(0条)

    保存