所有的字符串常量,默认都应在string.xml里定义。这样修改起来也方便。
R.string.xxx的值是自动生成的int型的Id值, 如何得到真正的string呢?
1. 在AndroidManifest.xml与layout里:
2.在activity里:
3.在其他地方如 ListAdapter,(必须有Context或Applicaton)
Java代码
- android:text="@string/resource_name"
Java代码
- this.getString(R.string.resource_name)
- getResources().getString(R.string.resource_name)
3.在其他地方如 ListAdapter,(必须有Context或Applicaton)
Java代码
- context.getString(R.string.resource_name)
- application.getString(R.string.resource_name)
最终代码:
List<String> classInfos = new ArrayList<>();
classInfos.add(getResources().getString(R.string.point_A1));
原文链接:http://blog.csdn.net/lizhenmingdirk/article/details/7369460
在《Android编程权威指南》书中第一个例子中,由于R.string.xxx的值是自动生成的int型的Id值,所以作者赋值给一个int型变量,在setText()等方法中直接调用这个int型变量,不再通过getResource().getString()繁琐的方法,提高了效率。所以上述最终代码可优化为:
List<Integer> classInfos = new ArrayList<>();
classInfos.add(R.string.point_A1);