0

我是一名初学Android Developer 我的程序可以编译,但是有一行不能正常工作。

这是行:

String m = R.string.mess2 + time/1000 + R.string.mess3;

变量“时间”是一个整数

这是 Strings.xml 文件,从中检索 R.string.mess2 和 R.string.mess3

<?xml version="1.0" encoding="utf-8"?>
<resources>
   <string name="mess2">Amazing, you read the End User License Agreement in </string>
   <string name="mess3"> seconds, go back and read it!</string>        
   </resources>

当应用程序启动时,字符串应该在哪里发生非常长的十进制。

这通常适用于 As3 或 C++,但在这里不起作用。

还有一件事,根据这些响应,您制作了一个这样的数组:

String[] MyString;

那么 [ ] 放在变量类型而不是名称中?

4

5 回答 5

2

您应该使用以下代码来获取资源:

getResources().getString(R.string.someid)

否则,您使用的是资源的整数标识符,而不是 xml 中的字符串。

所以在你的情况下应该是

String m = getResources().getString(R.string.mess2) + time/1000 + getResources().getString(R.string.mess3);
于 2013-11-15T02:39:55.990 回答
1

R.string.mess2 是 ResourceId。

你应该使用

getString(R.string.mess2) 从 Android 中的 string.xml 获取字符串。

于 2013-11-15T02:44:03.243 回答
1

你可以试试

<string name="mess">Amazing, you read the End User License Agreement in %d seconds, go back and read it!</string>    

而java代码将是

String m = String.format(context.getResources().getString(R.string.mess), time/1000);
于 2013-11-15T04:10:37.547 回答
0

您需要使用:

YourActivity.this.getString(R.string.mess2)+time/1000 + YourActivity.this.getString(R.string.mess2);

希望这可以帮助。

于 2013-11-15T04:23:39.387 回答
0

在 android 中使用 getResources 是一个好习惯,有时您可以忽略警告,它会正常工作。

于 2013-11-15T04:26:06.133 回答