[英]Translate Toast message android studio
Does anyone know to can I translate a toast message in android studio? 有谁知道我可以在Android工作室翻译一个Toast消息吗? I don't know how to convert the string message into the resource file (String.xml) like a textview in android studio 我不知道如何将字符串消息转换为资源文件(String.xml),就像android studio中的textview一样
String toastMessage1 = "Good Job! ";
String toastMessage2 = "Correct Answers";
if (dNumber >= 90) {
Toast.makeText(
MainActivity.this,
toastMessage1 + String.valueOf(dNumber) + " % " + toastMessage2,
Toast.LENGTH_LONG
).show();
}
You really should do your research first - it's quite simple, really. 你真的应该先做你的研究 - 这很简单,真的。 Read through String Resources guide - it has all the information you need. 阅读字符串资源指南 - 它包含您需要的所有信息。 In a nutshell, what you need is to add your text into the XML file as usual: 简而言之,您需要的是像往常一样将文本添加到XML文件中:
<string name="good_job">Good Job! %1$d %% Correct Answers</string>
Then you just use getResources
and getString
: 然后你只需使用getResources
和getString
:
String toastMessage = MainActivity.this.getResources().getString(R.string.good_job, dNumber);
Toast.makeText(MainActivity.this, toastMessage, Toast.LENGTH_LONG).show();
Do note that you need to have double-%, as the % char itself has special meaning inside the string and needs to be escaped. 请注意,您需要加倍%,因为%char本身在字符串中具有特殊含义,需要进行转义。
Add a string in your strings.xml 在strings.xml中添加一个字符串
<string name="string_name">Good Job! %d Correct Answers</string>
Use String functions for updating string. 使用String函数更新字符串。
String.format(res.getString(R.string.string_name), dNumber);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.