Eclipse/Java - R.string.* 中的值返回 int?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1983548/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 02:04:13  来源:igfitidea点击:

Eclipse/Java - Values in R.string.* return int?

javaandroidstringresources

提问by James Cadd

I thought I'd be classy and use the string.xml file to define some constant strings for things like exception messages. In strings.xml I hit Add, chose the "String" option (not 'String Array'), then gave it a name and value. I was surprised to see that this code doesn't work:

我以为我会很优雅,并使用 string.xml 文件为异常消息等内容定义一些常量字符串。在strings.xml 中,我点击Add,选择“String”选项(不是“String Array”),然后给它一个名称和值。我很惊讶地看到这段代码不起作用:

throw new Exception(R.string.MyExceptionMessage);

And that fails because R.string.MyExceptionMessage is actually of type int. I can verify that type by looking in R.java. What am I missing?

这失败了,因为 R.string.MyExceptionMessage 实际上是 int 类型。我可以通过查看 R.java 来验证该类型。我错过了什么?

采纳答案by Christopher Orr

Everything in the Rclass is a reference, hence it's just defined as an int.

R类中的所有内容都是引用,因此它只是定义为int.

If your code is running within — or has access to — an Android Context, you can call context.getString(R.string.my_exception_message)to get the actual Stringvalue.

如果您的代码在 Android 内运行或有权访问Context,您可以调用context.getString(R.string.my_exception_message)以获取实际String值。

Or, for things like exception strings that don't require to be translated and so don't strictly need to be defined in an Android resource .xml file, you could store the strings as constants in some sort of StringConstantsinterface. That way you can refer to the strings from within utility classes that may not have access to the Context.

或者,对于不需要翻译的异常字符串,因此不需要在 Android 资源 .xml 文件中严格定义,您可以将字符串作为常量存储在某种StringConstants接口中。这样您就可以从可能无法访问Context.

回答by PinoSan

Chris is right. But what if you want to localize Exception string too. For example you throw an Exception and when you catch it you want to show the exception message to the user. I would like to put them in strings.xml but at the same time I don't want to use this code

克里斯是对的。但是如果你也想本地化异常字符串怎么办。例如,您抛出一个异常,当您捕获它时,您希望向用户显示异常消息。我想将它们放在 strings.xml 中,但同时我不想使用此代码

throw new Exception(context.getString(R.string.my_exception_message))

because I think that if you got an Exception probably you don't want and you can't use some extra code to get the exception message.

因为我认为如果你得到一个异常你可能不想要并且你不能使用一些额外的代码来获取异常消息。

I think the only solution would be to declare a static String for this message in your main Activity and initialize it as soon as possible (for example in the onCreate() method) like this:

我认为唯一的解决方案是在您的主 Activity 中为此消息声明一个静态字符串并尽快对其进行初始化(例如在 onCreate() 方法中),如下所示:

private static String my_exception_message;

public onCreate()
{
    my_exception_message = context.getString(R.string.my_exception_message);
}

So when you throw the exception you have your localized message ready to use it. What you think? You can think of some other solutions?

因此,当您抛出异常时,您已准备好使用本地化消息。你认为呢?你能想到其他一些解决方案吗?

回答by skidadon

Use the "getResources().getString(id)" method.

使用“getResources().getString(id)”方法。

  getResources().getString(R.string.title_activity)

And boom, you have your string. Be sure to store that string in your string.xml file in the values folder.

繁荣,你有你的字符串。确保将该字符串存储在 values 文件夹中的 string.xml 文件中。