如何将字符串和整数连接成变量
这是一个真正愚蠢的问题,但我无法让它发挥作用。我使用了搜索选项,但找不到 Android 的答案。
我想要执行以下操作:
在 res/strings.xml 中,我有几个字符串
<string name="good0">blablabla</string>
<string name="good1">balablabla2</string>
etc
,我想在发生某些情况时随机显示这些字符串:
Toast.makeText(this,R.string.good+(Math.random()*10), Toast.LENGTH_LONG).show();
但这不起作用。
非常感谢您的帮助!
It is a real silly questions but I can't get it to work. I've used the search option, but couldn't not find my answer for android.
What I would like to do it the following:
In res/strings.xml i've got several strings
<string name="good0">blablabla</string>
<string name="good1">balablabla2</string>
etc
I want to show those strings randomly in a those when something happens:
Toast.makeText(this,R.string.good+(Math.random()*10), Toast.LENGTH_LONG).show();
But this doesn't work.
Thanks a lot for your help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用
字符串数组
。在
strings.xml
中:然后,在代码中您将得到如下内容:
Use a
String Array
.In
strings.xml
:Then, in code you will have something like:
R.string.good
是一个int
,因为它引用了一个 Resource。该 int 标识 XML 文件中的字符串。 Android 为其资源标识符提供了getString()
。有关字符串资源的 Android 文档
您必须获取字符串以这种方式从资源文件中取出,然后正常连接。
R.string.good
is anint
because it refers to a Resource. This int IDENTIFIES a string in an XML file. Android provides agetString()
for its resource identifiers.Android Docs on String Resources
You'll have to get the String out of the resource file this way, then concatenate as normal.
你不能那样做。
您必须使用
switch
块。You can't do that.
You will have to use a
switch
block.如果您有多个字符串值或整数值并将其存储在单个字符串中,那么字符串生成器是最适合的选择
操作类型,例如您有一个字符串数组,您想要
存储在单个字符串中,然后显示该字符串,然后使用它
方法。它会百分百起作用,而且非常适合
此类问题。**
在本例中,我将电话数组分配给单个字符串,并且
然后我会显示它等等...
If you have multiple string values or integer values and you to store it in a single string then String Builder is the best for this
type of operation, For Example you have a string array and you want to
store in a single string and then display this string then use this
method. it will work hundred percent and it is too much suitable for
this type of problems.**
here in this case i am assigning phone array to a single string and
then i will display it etc...