Android 如何在对象中使用资源字符串?-安卓
我在安卓系统中有这样的代码Android 如何在对象中使用资源字符串?-安卓,android,string,Android,String,我在安卓系统中有这样的代码 public static final String[] URLS = new String[]{"https://www.url1.com/","https://www.url2.com"}; public static final String[] URLS = new String[]{"@strings/url_1","@strings/url_2"}; 我想把这个代码变成这样 public static final String[] URLS = new
public static final String[] URLS = new String[]{"https://www.url1.com/","https://www.url2.com"};
public static final String[] URLS = new String[]{"@strings/url_1","@strings/url_2"};
我想把这个代码变成这样
public static final String[] URLS = new String[]{"https://www.url1.com/","https://www.url2.com"};
public static final String[] URLS = new String[]{"@strings/url_1","@strings/url_2"};
我尝试了太多的代码,但没有成功。我能怎么办?
非常感谢。@string资源由R.java文件中的int引用保存。所以,你不能直接使用它们。然而,还有另一种方法可以做到这一点 可以在arrays.xml文件中使用字符串数组。那就像
<string-array name="urls">
<item>https://www.url1.com/</item>
<item>https://www.url2.com/</item>
<item>https://www.url3.com/</item>
</string-array>
您需要使用从android资源获取字符串。请尝试以下操作
Resources res=context.getResources();
公共静态最终字符串[]url=新字符串[]{res.getString(R.String.url_1),res.getString(R.String.url_2)};