36

I'm trying to write some code that will reference a bool.xml file and will reference the current value inside the bool.

<bool name="enableQAurl">true</bool>

With this I want to be able to reference this in code, so that if it's set to True it does something and if false does something else. Just a simple If and else statement.

Any code references or feedback is greatly appreciated.

4

2 回答 2

79
 Resources res = getResources();
 boolean enableQAurl = res.getBoolean(R.bool.enableQAurl);

来源:http:
//developer.android.com/guide/topics/resources/more-resources.html

于 2013-04-19T21:18:43.977 回答
5

kaderud 的上述答案将完美运行。如果您不在活动中,则必须使用您的上下文。

如果您在片段或适配器中,那么您必须遵循以下内容。

boolean enableQAurl = context.getResources().getBoolean(R.bool.enableQAurl);
于 2016-11-03T06:56:43.543 回答